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.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name = "one2one_unidirect_association_fk_member") public class One2OneUnidirectAssociationFkMember implements Serializable{ private int oid; private String user_id; private String user_name; private String member_desc; private One2OneUnidirectAssociationFkSkill skill; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "oid") public int getOid() { return oid; } public void setOid(int oid) { this.oid = oid; } @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 @JoinColumn(name = "skill_id") public One2OneUnidirectAssociationFkSkill getSkill() { return skill; } public void setSkill(One2OneUnidirectAssociationFkSkill 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_fk_skill") public class One2OneUnidirectAssociationFkSkill implements Serializable{ private int skill_id; private String user_id; private String skill; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "skill_id") public int getSkill_id() { return skill_id; } public void setSkill_id(int skill_id) { this.skill_id = 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; public class One2OneUnidirectAssociationFkMyTestCaseA extends TestCase { public void test(){ Session session = HibernateAnnotationHelper.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); One2OneUnidirectAssociationFkMember member = new One2OneUnidirectAssociationFkMember(); member.setUser_id("ots520"); member.setUser_name("ots520"); member.setMember_desc("ots520 desc"); One2OneUnidirectAssociationFkSkill skill = new One2OneUnidirectAssociationFkSkill(); skill.setUser_id("ots520"); skill.setSkill("ots520 fk skill"); member.setSkill(skill); session.save(skill); session.save(member); One2OneUnidirectAssociationFkMember TestMember = (One2OneUnidirectAssociationFkMember) session.get(One2OneUnidirectAssociationFkMember.class, 2); One2OneUnidirectAssociationFkSkill unidirectAssociationFkSkill = TestMember.getSkill(); System.out.println("unidirectAssociationFkSkill=" + unidirectAssociationFkSkill.getSkill()); tx.commit(); session.close(); } }
沒有留言:
張貼留言