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_join_member")
public class One2ManyAndMany2OneBidirectAssociationJoinMember 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
// @JoinTable(name = "one2many_and_many2one_bidirect_association_join_member_skill",
// joinColumns = {
// //@JoinColumn(name="fk_member_id", unique = true) //若要 1 對 1的話, 加上 unique
// @JoinColumn(name="fk_member_id")
// },
// inverseJoinColumns = {
// @JoinColumn(name="fk_skill_id")
// }
// )
@OneToMany(mappedBy = "member")
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.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "one2many_and_many2one_bidirect_association_join_skill")
public class One2ManyAndMany2OneBidirectAssociationJoinSkill implements Serializable{
private int skillId;
private String userId;
private String skill;
private One2ManyAndMany2OneBidirectAssociationJoinMember 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(optional=true)
@JoinTable(name = "one2many_and_many2one_bidirect_association_join_member_skill",
joinColumns = {
@JoinColumn(name="fk_skill_id", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="fk_member_id")
}
)
public One2ManyAndMany2OneBidirectAssociationJoinMember getMember() {
return member;
}
public void setMember(One2ManyAndMany2OneBidirectAssociationJoinMember 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 One2ManyAndMany2OneBidirectAssociationJoinMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
One2ManyAndMany2OneBidirectAssociationJoinMember member = new One2ManyAndMany2OneBidirectAssociationJoinMember();
member.setUserId("ots520");
member.setUserName("ots520");
member.setMemberDesc("ots520 desc");
ListskillList = new ArrayList ();
One2ManyAndMany2OneBidirectAssociationJoinSkill skill1 = new One2ManyAndMany2OneBidirectAssociationJoinSkill();
skill1.setUserId("ots520");
skill1.setSkill("skill1");
skill1.setMember(member);
session.save(skill1);
skillList.add(skill1);
One2ManyAndMany2OneBidirectAssociationJoinSkill skill2 = new One2ManyAndMany2OneBidirectAssociationJoinSkill();
skill2.setUserId("ots520");
skill2.setSkill("skill2");
skill2.setMember(member);
session.save(skill2);
skillList.add(skill2);
member.setSkillList(skillList);
session.save(member);
One2ManyAndMany2OneBidirectAssociationJoinMember JoinMember = (One2ManyAndMany2OneBidirectAssociationJoinMember) session.get(One2ManyAndMany2OneBidirectAssociationJoinMember.class, 3);
if(!(JoinMember == null)){
Listlist = JoinMember.getSkillList();
for (One2ManyAndMany2OneBidirectAssociationJoinSkill one2ManyAndMany2OneBidirectAssociationJoinSkill : list) {
System.out.println("one2ManyAndMany2OneBidirectAssociationJoinSkill=" + one2ManyAndMany2OneBidirectAssociationJoinSkill.getSkillId());
}
}
One2ManyAndMany2OneBidirectAssociationJoinSkill JoinSkill = (One2ManyAndMany2OneBidirectAssociationJoinSkill) session.get(One2ManyAndMany2OneBidirectAssociationJoinSkill.class, 4);
if(!(JoinSkill == null)){
One2ManyAndMany2OneBidirectAssociationJoinMember JoinSkillMember = JoinSkill.getMember();
System.out.println("JoinSkillMember=" + JoinSkillMember.getMemberId());
}
tx.commit();
session.close();
}
}
0 意見:
張貼留言