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.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
@Entity
@Table(name = "many2many_bidirect_association_join_member")
public class Many2ManyBidirectAssociationJoinMember 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;
}
//@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@ManyToMany
@org.hibernate.annotations.Cascade( value = org.hibernate.annotations.CascadeType.SAVE_UPDATE )
@JoinTable(name = "many2many_bidirect_association_join_member_skill",
joinColumns = {
@JoinColumn(name="fk_member_id")
},
inverseJoinColumns = {
@JoinColumn(name="fk_skill_id")
}
)
public ListgetSkillList() {
return skillList;
}
public void setSkillList(ListskillList) {
this.skillList = skillList;
}
}
Skill
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.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "many2many_bidirect_association_join_skill")
public class Many2ManyBidirectAssociationJoinSkill implements Serializable{
private int skillId;
private String userId;
private String skill;
private ListmemberList;
@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;
}
// @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="skillList")
@ManyToMany(mappedBy="skillList")
@org.hibernate.annotations.Cascade( value = org.hibernate.annotations.CascadeType.SAVE_UPDATE )
public ListgetMemberList() {
return memberList;
}
public void setMemberList(
ListmemberList) {
this.memberList = memberList;
}
}
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 Many2ManyBidirectAssociationJoinMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
ListmemberList = new ArrayList ();
ListskillList = new ArrayList ();
Many2ManyBidirectAssociationJoinMember member1 = new Many2ManyBidirectAssociationJoinMember();
member1.setUserId("ots520");
member1.setUserName("ots520");
member1.setMemberDesc("ots520 desc");
memberList.add(member1);
Many2ManyBidirectAssociationJoinMember member2 = new Many2ManyBidirectAssociationJoinMember();
member2.setUserId("todd");
member2.setUserName("todd name");
member2.setMemberDesc("todd desc");
memberList.add(member2);
Many2ManyBidirectAssociationJoinSkill skill1 = new Many2ManyBidirectAssociationJoinSkill();
skill1.setUserId("none");
skill1.setSkill("skill1");
skill1.setMemberList(memberList);
skillList.add(skill1);
Many2ManyBidirectAssociationJoinSkill skill2 = new Many2ManyBidirectAssociationJoinSkill();
skill2.setUserId("none");
skill2.setSkill("skill2");
skill2.setMemberList(memberList);
skillList.add(skill2);
skill1.setMemberList(memberList);
skill2.setMemberList(memberList);
member1.setSkillList(skillList);
member2.setSkillList(skillList);
session.save(member1);
session.save(member2);
tx.commit();
session.close();
}
}
0 意見:
張貼留言