package com.todd.bean; import java.io.Serializable; import java.util.List; 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.Table; @Entity @Table(name = "one2many_unidirect_association_fk_member") public class One2ManyUnidirectAssociationFkMember implements Serializable{ private int memberId; private String userId; private String userName; private String memberDesc; private ListskillList; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "member_id") public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } @Column(name = "user_id") public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } @Column(name = "user_name") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name = "member_desc") public String getMemberDesc() { return memberDesc; } public void setMemberDesc(String memberDesc) { this.memberDesc = memberDesc; } @OneToMany @JoinColumn(name = "member_id") // watch out! public List getSkillList() { return skillList; } public void setSkillList(List skillList) { this.skillList = skillList; } }
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 = "one2many_unidirect_association_fk_skill") public class One2ManyUnidirectAssociationFkSkill implements Serializable{ private int skillId; private String userId; private String skill; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "skill_id") public int getSkillId() { return skillId; } public void setSkillId(int skillId) { this.skillId = skillId; } @Column(name = "user_id") public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } @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. * 2. * @author Todd */ public class One2ManyUnidirectAssociationFkMyTestCaseA extends TestCase { public void test(){ Session session = HibernateAnnotationHelper.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); One2ManyUnidirectAssociationFkMember member = new One2ManyUnidirectAssociationFkMember(); member.setUserId("ots520"); member.setUserName("ots520"); member.setMemberDesc("ots520 desc"); ListskillList = new ArrayList (); One2ManyUnidirectAssociationFkSkill skill1 = new One2ManyUnidirectAssociationFkSkill(); skill1.setUserId("ots520"); skill1.setSkill("skill1"); session.save(skill1); skillList.add(skill1); One2ManyUnidirectAssociationFkSkill skill2 = new One2ManyUnidirectAssociationFkSkill(); skill2.setUserId("ots520"); skill2.setSkill("skill2"); session.save(skill2); skillList.add(skill2); member.setSkillList(skillList); session.save(member); One2ManyUnidirectAssociationFkMember JoinMember = (One2ManyUnidirectAssociationFkMember) session.get(One2ManyUnidirectAssociationFkMember.class, 1); if(!(JoinMember == null)){ List list = JoinMember.getSkillList(); for (One2ManyUnidirectAssociationFkSkill one2ManyUnidirectAssociationFkSkill : list) { System.out.println("one2ManyUnidirectAssociationFkSkill=" + one2ManyUnidirectAssociationFkSkill.getSkillId()); } } tx.commit(); session.close(); } }
沒有留言:
張貼留言