package com.todd.bean;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "one2one_unidirect_association_pk_member")
public class One2OneUnidirectAssociationPkMember implements Serializable{
private String user_id;
private String user_name;
private String member_desc;
private One2OneUnidirectAssociationPkSkill skill;
@Id
@Column(name = "user_id")
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@Column(name = "user_name")
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
@Column(name = "member_desc")
public String getMember_desc() {
return member_desc;
}
public void setMember_desc(String member_desc) {
this.member_desc = member_desc;
}
@OneToOne
@PrimaryKeyJoinColumn
public One2OneUnidirectAssociationPkSkill getSkill() {
return skill;
}
public void setSkill(One2OneUnidirectAssociationPkSkill skill) {
this.skill = skill;
}
}
Skill
package com.todd.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "one2one_unidirect_association_pk_skill")
public class One2OneUnidirectAssociationPkSkill implements Serializable{
private String user_id;
private String skill;
@Id
@Column(name = "user_id")
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@Column(name = "skill")
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
}
Test
package com.todd.bean;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import junit.framework.TestCase;
import com.todd.HibernateAnnotationHelper;
/**
* 1.讓主鍵對映就行
* @author Todd
*/
public class One2OneUnidirectAssociationPkMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
One2OneUnidirectAssociationPkMember member = new One2OneUnidirectAssociationPkMember();
member.setUser_id("ots520");
member.setUser_name("ots520");
member.setMember_desc("ots520 desc");
One2OneUnidirectAssociationPkSkill skill = new One2OneUnidirectAssociationPkSkill();
skill.setUser_id("ots520");
skill.setSkill("ass");
member.setSkill(skill);
session.save(member);
session.save(skill);
One2OneUnidirectAssociationPkMember pkMember = (One2OneUnidirectAssociationPkMember) session.get(One2OneUnidirectAssociationPkMember.class, "ots520");
System.out.println("member desc =" + pkMember.getMember_desc());
tx.commit();
session.close();
}
}
0 意見:
張貼留言