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.JoinTable; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name = "one2one_bidirect_association_join_member") public class One2OneBidirectAssociationJoinMember implements Serializable{ private int member_id; private String user_id; private String user_name; private String member_desc; private One2OneBidirectAssociationJoinSkill skill; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "member_id") public int getMember_id() { return member_id; } public void setMember_id(int member_id) { this.member_id = member_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(optional = true) @JoinTable(name = "one2one_bidirect_association_join_member_skill", joinColumns = { @JoinColumn(name = "fk_member_id", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "fk_skill_id") } ) public One2OneBidirectAssociationJoinSkill getSkill() { return skill; } public void setSkill(One2OneBidirectAssociationJoinSkill 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.OneToOne; import javax.persistence.Table; @Entity @Table(name = "one2one_bidirect_association_join_skill") public class One2OneBidirectAssociationJoinSkill implements Serializable{ private int skill_id; private String user_id; private String skill; private One2OneBidirectAssociationJoinMember member; @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; } @OneToOne(optional=true, mappedBy="skill") // pointing to Member's skill field public One2OneBidirectAssociationJoinMember getMember() { return member; } public void setMember(One2OneBidirectAssociationJoinMember member) { this.member = member; } }
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.join 的仲介table, 不需有實體bean, 會自己產生 * 2. * @author Todd */ public class One2OneBidirectAssociationJoinMyTestCaseA extends TestCase { public void test(){ Session session = HibernateAnnotationHelper.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); One2OneBidirectAssociationJoinMember member = new One2OneBidirectAssociationJoinMember(); member.setUser_id("ots520"); member.setUser_name("ots520"); member.setMember_desc("ots520 member desc"); One2OneBidirectAssociationJoinSkill skill = new One2OneBidirectAssociationJoinSkill(); skill.setUser_id("ots520"); skill.setSkill("ass"); member.setSkill(skill); skill.setMember(member); session.save(member); session.save(skill); One2OneBidirectAssociationJoinMember JoinMember = (One2OneBidirectAssociationJoinMember) session.get(One2OneBidirectAssociationJoinMember.class, 1); System.out.println("member desc =" + JoinMember.getSkill().getSkill_id()); One2OneBidirectAssociationJoinSkill JoinSkill = (One2OneBidirectAssociationJoinSkill) session.get(One2OneBidirectAssociationJoinSkill.class, 1); System.out.println("member desc =" + JoinSkill.getMember().getMember_desc()); tx.commit(); session.close(); } }
沒有留言:
張貼留言