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.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "one2many_unidirect_association_join_member")
public class One2ManyUnidirectAssociationJoinMember implements Serializable{
private int member_id;
private String user_id;
private String user_name;
private String member_desc;
private ListskillList;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "member_id")
public int getMember_id() {
return member_id;
}
public void setMember_id(int member_id) {
this.member_id = member_id;
}
@Column(name = "user_id")
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@Column(name = "user_name")
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
@Column(name = "member_desc")
public String getMember_desc() {
return member_desc;
}
public void setMember_desc(String member_desc) {
this.member_desc = member_desc;
}
@OneToMany
@JoinTable(name = "one2many_unidirect_association_join_member_skill",
joinColumns = {
//@JoinColumn(name="fk_member_id", unique = true) //錯的
@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 = "one2many_unidirect_association_join_skill")
public class One2ManyUnidirectAssociationJoinSkill implements Serializable{
private int skill_id;
private String user_id;
private String skill;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "skill_id")
public int getSkill_id() {
return skill_id;
}
public void setSkill_id(int skill_id) {
this.skill_id = skill_id;
}
@Column(name = "user_id")
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@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 One2ManyUnidirectAssociationJoinMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
One2ManyUnidirectAssociationJoinMember member = new One2ManyUnidirectAssociationJoinMember();
member.setUser_id("ots520");
member.setUser_name("ots520");
member.setMember_desc("ots520 desc");
ListskillList = new ArrayList ();
One2ManyUnidirectAssociationJoinSkill skill1 = new One2ManyUnidirectAssociationJoinSkill();
skill1.setUser_id("ots520");
skill1.setSkill("ass1");
session.save(skill1);
skillList.add(skill1);
One2ManyUnidirectAssociationJoinSkill skill2 = new One2ManyUnidirectAssociationJoinSkill();
skill2.setUser_id("ots5202");
skill2.setSkill("ass2");
session.save(skill2);
skillList.add(skill2);
member.setSkillList(skillList);
session.save(member);
One2ManyUnidirectAssociationJoinMember JoinMember = (One2ManyUnidirectAssociationJoinMember) session.get(One2ManyUnidirectAssociationJoinMember.class, 3);
if(!(JoinMember == null)){
Listlist = JoinMember.getSkillList();
for (One2ManyUnidirectAssociationJoinSkill one2ManyUnidirectAssociationJoinSkill : list) {
System.out.println("one2ManyUnidirectAssociationJoinSkill=" + one2ManyUnidirectAssociationJoinSkill.getSkill_id());
}
}
tx.commit();
session.close();
}
}
0 意見:
張貼留言