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.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "many2one_unidirect_association_join_member") public class Many2OneUnidirectAssociationJoinMember implements Serializable{ private int member_id; private String user_id; private String user_name; private String member_desc; private Many2OneUnidirectAssociationJoinSkill 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; } @ManyToOne(optional=true) @JoinTable(name = "many2one_unidirect_association_join_member_skill", joinColumns = { @JoinColumn(name="fk_member_id", unique = true) }, inverseJoinColumns = { @JoinColumn(name="fk_skill_id") } ) public Many2OneUnidirectAssociationJoinSkill getSkill() { return skill; } public void setSkill(Many2OneUnidirectAssociationJoinSkill 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 = "many2one_unidirect_association_join_skill") public class Many2OneUnidirectAssociationJoinSkill 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.ArrayList; 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.多個member 有共同的 skill時 * 2. * @author Todd */ public class Many2OneUnidirectAssociationJoinMyTestCaseA extends TestCase { public void test(){ Session session = HibernateAnnotationHelper.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Many2OneUnidirectAssociationJoinMember member1 = new Many2OneUnidirectAssociationJoinMember(); member1.setUser_id("ots520"); member1.setUser_name("ots520"); member1.setMember_desc("ots520 desc1"); Many2OneUnidirectAssociationJoinMember member2 = new Many2OneUnidirectAssociationJoinMember(); member2.setUser_id("todd"); member2.setUser_name("todd"); member2.setMember_desc("ots520 desc2"); Many2OneUnidirectAssociationJoinSkill skill = new Many2OneUnidirectAssociationJoinSkill(); skill.setUser_id("這情況應該不用有 user_id了吧"); skill.setSkill("ass"); session.save(skill); member1.setSkill(skill); member2.setSkill(skill); session.save(member1); session.save(member2); Many2OneUnidirectAssociationJoinMember JoinMember = (Many2OneUnidirectAssociationJoinMember) session.get(Many2OneUnidirectAssociationJoinMember.class, 7); if(!(JoinMember == null)){ System.out.println("JoinMember=" + JoinMember.getSkill().getSkill_id()); } tx.commit(); session.close(); } }
沒有留言:
張貼留言