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 = "one2many_and_many2one_bidirect_association_member")
public class One2ManyAndMany2OneBidirectAssociationMember 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(mappedBy="member") // 對應到對方的 field喔 , 不是column name
@Column(name="skill_id") // inverse=true
public ListgetSkillList() {
return skillList;
}
public void setSkillList(
ListskillList) {
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_and_many2one_bidirect_association_skill")
public class One2ManyAndMany2OneBidirectAssociationSkill implements Serializable{
private int skillId;
private String userId;
private String skill;
private One2ManyAndMany2OneBidirectAssociationMember member;
@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;
}
@ManyToOne
@JoinColumn(name="member_id") // inverse = false , 對應到對方的 id column name
public One2ManyAndMany2OneBidirectAssociationMember getMember() {
return member;
}
public void setMember(One2ManyAndMany2OneBidirectAssociationMember member) {
this.member = member;
}
}
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.雙向的話, 只要把One2Many 或 Many2One 對調就行
* 2.
* @author Todd
*/
public class One2ManyAndMany2OneBidirectAssociationMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
One2ManyAndMany2OneBidirectAssociationMember member = new One2ManyAndMany2OneBidirectAssociationMember();
member.setUserId("ots520");
member.setUserName("ots520");
member.setMemberDesc("ots520 desc");
ListskillList = new ArrayList ();
One2ManyAndMany2OneBidirectAssociationSkill skill1 = new One2ManyAndMany2OneBidirectAssociationSkill();
skill1.setUserId("ots520");
skill1.setSkill("ass1");
skill1.setMember(member);
session.save(skill1);
skillList.add(skill1);
One2ManyAndMany2OneBidirectAssociationSkill skill2 = new One2ManyAndMany2OneBidirectAssociationSkill();
skill2.setUserId("ots520");
skill2.setSkill("ass2");
skill2.setMember(member);
session.save(skill2);
skillList.add(skill2);
member.setSkillList(skillList);
session.save(member);
One2ManyAndMany2OneBidirectAssociationMember JoinMember = (One2ManyAndMany2OneBidirectAssociationMember) session.get(One2ManyAndMany2OneBidirectAssociationMember.class, 1);
if(!(JoinMember == null)){
Listlist = JoinMember.getSkillList();
for (One2ManyAndMany2OneBidirectAssociationSkill one2ManyAndMany2OneBidirectAssociationSkill : list) {
System.out.println("one2ManyAndMany2OneBidirectAssociationSkill=" + one2ManyAndMany2OneBidirectAssociationSkill.getSkillId());
}
}
One2ManyAndMany2OneBidirectAssociationSkill JoinSkill = (One2ManyAndMany2OneBidirectAssociationSkill) session.get(One2ManyAndMany2OneBidirectAssociationSkill.class, 4);
if(!(JoinSkill == null)){
One2ManyAndMany2OneBidirectAssociationMember JoinSkillMember = JoinSkill.getMember();
System.out.println("JoinSkillMember=" + JoinSkillMember.getMemberId());
}
tx.commit();
session.close();
}
}
0 意見:
張貼留言