@注解_复合主键

直接上代码咯

package cn.jbit.test2.bean;

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
public class UserId implements Serializable {

	private static final long serialVersionUID = 9114000181591317544L;
	
	private String fld_firstName;
	private String fld_lastName;
	
	public UserId() {
		super();
	}
	
	public UserId(String fldFirstName, String fldLastName) {
		super();
		fld_firstName = fldFirstName;
		fld_lastName = fldLastName;
	}
	
	public String getFld_firstName() {
		return fld_firstName;
	}

	public void setFld_firstName(String fldFirstName) {
		fld_firstName = fldFirstName;
	}

	public String getFld_lastName() {
		return fld_lastName;
	}

	public void setFld_lastName(String fldLastName) {
		fld_lastName = fldLastName;
	}
}
package cn.jbit.test2.bean;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.MapsId;
import javax.persistence.Table;

@Entity
@Table(name = "test_user4")
public class User implements Serializable {

	private static final long serialVersionUID = -5935733628499862785L;
	
	@MapsId("userId")
	@EmbeddedId
	@AttributeOverrides({
		@AttributeOverride(name = "firstName", column = @Column(name = "fld_firstName")),
		@AttributeOverride(name = "lastName", column = @Column(name = "fld_lastName"))
	})
	private UserId userId;
	private Integer age;

	public User() {
		super();
	}

	public User(UserId userId, Integer age) {
		super();
		this.userId = userId;
		this.age = age;
	}

	public UserId getId() {
		return userId;
	}

	public void setId(UserId userId) {
		this.userId = userId;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
}
用户测试类:

package cn.jbit.test2.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.jbit.ch04.util.HibernateUtil;
import cn.jbit.test2.bean.User;
import cn.jbit.test2.bean.UserId;

public class UserTest {

	public static void main(String[] args) {
		Session session = null;
		Transaction tx = null;
		
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			UserId userId = new UserId();
			userId.setFld_firstName("gu");
			userId.setFld_lastName("yuanyuan");
			
			User user = new User();
			user.setId(userId);
			user.setAge(20);
			session.save(user);
			
			tx.commit();
		} catch (HibernateException e) {
			if (tx.isActive()) {
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
}
package cn.jbit.test2.bean;

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
public class CustomerId implements Serializable{

	private static final long serialVersionUID = 5472306219147740054L;
	
	private UserId userId;
	private String customerNumber;

	public CustomerId() {
		super();
	}

	public CustomerId(UserId userId, String customerNumber) {
		super();
		this.userId = userId;
		this.customerNumber = customerNumber;
	}

	public UserId getUserId() {
		return userId;
	}

	public void setUserId(UserId userId) {
		this.userId = userId;
	}

	public String getCustomerNumber() {
		return customerNumber;
	}

	public void setCustomerNumber(String customerNumber) {
		this.customerNumber = customerNumber;
	}
}
package cn.jbit.test2.bean;

import java.io.Serializable;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "test_customer")
public class Customer implements Serializable {

	private static final long serialVersionUID = 8741866719324162345L;

	@EmbeddedId
	private CustomerId customerId;
	private String preferredCustomer;
	
	@MapsId("userId")//userId与customerId.userId使用相同的值
	@JoinColumns( {
			@JoinColumn(name = "userfirstname_fk", referencedColumnName = "fld_firstName"),
			@JoinColumn(name = "userlastname_fk", referencedColumnName = "fld_lastName") })
	@OneToOne
	private User user;

	public Customer() {
		super();
	}

	public Customer(CustomerId customerId, String preferredCustomer, User user) {
		super();
		this.customerId = customerId;
		this.preferredCustomer = preferredCustomer;
		this.user = user;
	}

	public CustomerId getCustomerId() {
		return customerId;
	}

	public void setCustomerId(CustomerId customerId) {
		this.customerId = customerId;
	}

	public String getPreferredCustomer() {
		return preferredCustomer;
	}

	public void setPreferredCustomer(String preferredCustomer) {
		this.preferredCustomer = preferredCustomer;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
测试类:

package cn.jbit.test2.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.jbit.ch04.util.HibernateUtil;
import cn.jbit.test2.bean.Customer;
import cn.jbit.test2.bean.CustomerId;
import cn.jbit.test2.bean.User;
import cn.jbit.test2.bean.UserId;

public class CustomerTest {
	
	public static void main(String[] args) {
		Session session = null;
		Transaction tx = null;
		
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			UserId userId = new UserId();
			userId.setFld_firstName("gu");
			userId.setFld_lastName("yuanyuan");
			
			User user = (User)session.get(User.class, userId);//获取用户
			
			CustomerId customerId = new CustomerId();
			customerId.setUserId(userId);
			customerId.setCustomerNumber("abcd");
			
			Customer customer = new Customer();
			customer.setCustomerId(customerId);
			customer.setPreferredCustomer("张三");
			customer.setUser(user);
			
			session.save(customer);
			
			tx.commit();
		} catch (HibernateException e) {
			if (tx.isActive()) {
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值