Switch to full style
Java persistent API
Post a reply

Join Column by Primary Key

Sat Apr 17, 2010 9:48 pm

The following code represents a one-to-one relation between person table and bed table (" Actually i want to sleep now :) ") . The primary key of person is used as the key of bed entity .

Person entity class . (Notice the annotation of one-to-one )
Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@
Entity
@Table(name="PERSON")
public class 
Person implements java.io.Serializable{
    
/**
     * 
     */
    
private static final long serialVersionUID 1L;
    @
Id
    
private String Id;
    private 
String name;
    private 
Date birthday;
    private 
String gender;
    
    @
OneToOne(mappedBy="person"
    private 
Bed bed;
    
    public 
String getId() {
        return 
Id;
    }
    public 
void setId(String id) {
        
Id id;
    }
    @
Column(name="NAME")
    public 
String getName() {
        return 
name;
    }
    public 
void setName(String name) {
        
this.name name;
    }
    @
Column(name="BIRTHDAT")
    public 
Date getBirthday() {
        return 
birthday;
    }
    public 
void setBirthday(Date birthday) {
        
this.birthday birthday;
    }
    @
Column(name="GENDER")
    public 
String getGender() {
        return 
gender;
    }
    public 
void setGender(String gender) {
        
this.gender gender;
    }
    public 
void setBed(Bed bed) {
    
this.bed bed;
    }
    public 
Bed getBed() {
    return 
bed;
    }
 
    
}



 


@PrimaryKeyJoinColumn annotation is used to specify the relation .

Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@
Entity
@Table(name="BED")
public class 
Bed implements java.io.Serializable{
    
/**
     * 
     */
    
private static final long serialVersionUID 1L;
    @
Id
    
private String Id;
    private 
String type;
    private 
Date production_date;
    private 
String width;
    private 
String height;
    @
OneToOne
    
@PrimaryKeyJoinColumn
    
private Person person;
    public 
String getId() {
        return 
Id;
    }
    public 
void setId(String id) {
        
Id id;
    }
     
    @
Column(name="TYPE")
    public 
String getType() {
        return 
type;
    }
    
    public 
void setType(String type) {
        
this.type type;
    }
    @
Column(name="PRODUCTION")
    public 
Date getProduction_date() {
        return 
production_date;
    }
    public 
void setProduction_date(Date production_date) {
        
this.production_date production_date;
    }
    @
Column(name="WIDTH")
    public 
String getWidth() {
        return 
width;
    }
    public 
void setWidth(String width) {
        
this.width width;
    }
    @
Column(name="HEIGHT")
    public 
String getHeight() {
        return 
height;
    }
    public 
void setHeight(String height) {
        
this.height height;
    }
    public 
void setPerson(Person person) {
    
this.person person;
    }
    public 
Person getPerson() {
    return 
person;
    }
    
    
}

 




Post a reply
  Related Posts  to : Join Column by Primary Key
 Get column alias in php     -  
 column can't be null     -  
 Get table column flag in php     -  
 Create auto numbered column in mysql query     -  

Topic Tags

Java JPA