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.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "many2many_unidirect_association_join_member")
public class Many2ManyUnidirectAssociationJoinMember 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
@JoinTable(name = "many2many_unidirect_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 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 = "many2many_unidirect_association_join_skill")
public class Many2ManyUnidirectAssociationJoinSkill 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 Many2ManyUnidirectAssociationJoinMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Many2ManyUnidirectAssociationJoinMember member1 = new Many2ManyUnidirectAssociationJoinMember();
member1.setUserId("ots520");
member1.setUserName("ots520");
member1.setMemberDesc("ots520 desc");
Many2ManyUnidirectAssociationJoinMember member2 = new Many2ManyUnidirectAssociationJoinMember();
member2.setUserId("todd");
member2.setUserName("todd name");
member2.setMemberDesc("todd desc");
ListskillList = new ArrayList ();
Many2ManyUnidirectAssociationJoinSkill skill1 = new Many2ManyUnidirectAssociationJoinSkill();
skill1.setUserId("none");
skill1.setSkill("skill1");
session.save(skill1);
skillList.add(skill1);
Many2ManyUnidirectAssociationJoinSkill skill2 = new Many2ManyUnidirectAssociationJoinSkill();
skill2.setUserId("none");
skill2.setSkill("skill2");
session.save(skill2);
skillList.add(skill2);
member1.setSkillList(skillList);
member2.setSkillList(skillList);
session.save(member1);
session.save(member2);
tx.commit();
session.close();
}
}
0 意見:
張貼留言