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.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name = "one2one_unidirect_association_join_member") public class One2OneUnidirectAssociationJoinMember implements Serializable{ private int member_id; private String user_id; private String user_name; private String member_desc; private One2OneUnidirectAssociationJoinSkill skill; @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; } @OneToOne(optional = true) @JoinTable(name = "one2one_unidirect_association_join_member_skill", joinColumns = { @JoinColumn(name = "fk_member_id", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "fk_skill_id") } ) public One2OneUnidirectAssociationJoinSkill getSkill() { return skill; } public void setSkill(One2OneUnidirectAssociationJoinSkill skill) { this.skill = skill; } }
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 = "one2one_unidirect_association_join_skill") public class One2OneUnidirectAssociationJoinSkill 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.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.join 的仲介table, 不需有實體bean, 會自己產生
* 2.
* @author Todd
*/
public class One2OneUnidirectAssociationJoinMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
One2OneUnidirectAssociationJoinMember member = new One2OneUnidirectAssociationJoinMember();
member.setUser_id("ots520");
member.setUser_name("ots520");
member.setMember_desc("ots520 desc");
One2OneUnidirectAssociationJoinSkill skill = new One2OneUnidirectAssociationJoinSkill();
skill.setUser_id("ots520");
skill.setSkill("ass");
member.setSkill(skill);
session.save(member);
session.save(skill);
One2OneUnidirectAssociationJoinMember JoinMember = (One2OneUnidirectAssociationJoinMember) session.get(One2OneUnidirectAssociationJoinMember.class, 1);
System.out.println("member desc =" + JoinMember.getMember_desc());
tx.commit();
session.close();
}
}
沒有留言:
張貼留言