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.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "many2one_unidirect_association_fk_member")
public class Many2OneUnidirectAssociationFkMember implements Serializable{
private int memberId;
private String userId;
private String userName;
private String memberDesc;
private Many2OneUnidirectAssociationFkSkill skill;
@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;
}
@ManyToOne
@JoinColumn(name = "skill_id") // watch out! 指定 一方 的對應key
public Many2OneUnidirectAssociationFkSkill getSkill() {
return skill;
}
public void setSkill(Many2OneUnidirectAssociationFkSkill 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 = "many2one_unidirect_association_fk_skill")
public class Many2OneUnidirectAssociationFkSkill 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 Many2OneUnidirectAssociationFkMyTestCaseA extends TestCase {
public void test(){
Session session = HibernateAnnotationHelper.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Many2OneUnidirectAssociationFkSkill skill = new Many2OneUnidirectAssociationFkSkill();
skill.setUserId("這情況就不須這欄位了吧");
skill.setSkill("skill");
session.save(skill);
Many2OneUnidirectAssociationFkMember member1 = new Many2OneUnidirectAssociationFkMember();
member1.setUserId("ots520");
member1.setUserName("ots520");
member1.setMemberDesc("ots520 desc");
member1.setSkill(skill);
Many2OneUnidirectAssociationFkMember member2 = new Many2OneUnidirectAssociationFkMember();
member2.setUserId("todd");
member2.setUserName("todd name");
member2.setMemberDesc("todd desc");
member2.setSkill(skill);
session.save(member1);
session.save(member2);
tx.commit();
session.close();
}
}
0 意見:
張貼留言