gpt4 book ai didi

java - 如何在Hibernate中将主键从主表复制到子表

转载 作者:行者123 更新时间:2023-12-02 10:19:14 26 4
gpt4 key购买 nike

如何在 Hibernate 中将主键(共享键)从主表复制到子表。我正在尝试进行单向映射(Employee 引用了Address,反之亦然)。

附加信息: hibernate 依赖:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>

我有 Employee 类和 Address 实体,如下所示。我正在尝试在 Employee 实体中生成主键到 Address 实体。

但是,我看到 ADDRESS 表中的 employeeId 始终由 hibernate 生成为 0(我希望复制它)。我想我在使用 @PrimaryKeyJoinColumn@MapsId 时会做错。我尝试过添加两者,或者添加其中之一,但没有成功。

以下是对应表的架构:

create table EMPLOYEE(
EMPLOYEE_ID int auto_increment,
NAME varchar(100),
SALARY double,
primary key(EMPLOYEE_ID)
);



create table ADDRESS
(
EMPLOYEE_ID int references EMPLOYEE.EMPLOYEE_ID,
STREET_NAME varchar(40),
CITY_NAME varchar(40),
STATE_NAME varchar(40),
ZIP_CODE varchar(40),
primary key(EMPLOYEE_ID)
);

实体映射如下所示:

员工.java

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.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import java.io.Serializable;

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

@Id
@Column(name = "EMPLOYEE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "NAME")
private String name;

@Column(name = "SALARY")
private double salary;

@OneToOne
@JoinColumn(name="EMPLOYEE_ID")
@MapsId
//@PrimaryKeyJoinColumn
private Address address;

public int getId() {
return id;
}

public Employee setId(int id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public Employee setName(String name) {
this.name = name;
return this;
}

public double getSalary() {
return salary;
}

public Employee setSalary(double salary) {
this.salary = salary;
return this;
}

public Address getAddress() {
return address;
}

public Employee setAddress(Address address) {
this.address = address;
return this;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", address=" + address +
'}';
}
}

地址.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapsId;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import java.io.Serializable;

@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {

@Id
@Column(name = "EMPLOYEE_ID")
private int id;

@Column(name = "STREET_NAME")
private String streetName;

@Column(name = "CITY_NAME")
private String cityName;

@Column(name = "STATE_NAME")
private String stateName;

@Column(name = "ZIP_CODE")
private String zipCode;

public int getId() {
return id;
}

public Address setId(int id) {
this.id = id;
return this;
}

public String getStreetName() {
return streetName;
}

public Address setStreetName(String streetName) {
this.streetName = streetName;
return this;
}

public String getCityName() {
return cityName;
}

public Address setCityName(String cityName) {
this.cityName = cityName;
return this;
}

public String getStateName() {
return stateName;
}

public Address setStateName(String stateName) {
this.stateName = stateName;
return this;
}

public String getZipCode() {
return zipCode;
}

public Address setZipCode(String zipCode) {
this.zipCode = zipCode;
return this;
}

@Override
public String toString() {
return "Address{" +
"id=" + id +
", streetName='" + streetName + '\'' +
", cityName='" + cityName + '\'' +
", stateName='" + stateName + '\'' +
", zipCode='" + zipCode + '\'' +
'}';
}
}

保存方法:

  public Employee save(Employee employee){

Session session = HibernateUtil.buildSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(address); // Should I do this?
session.save(employee);
transaction.commit();
return employee;
}

最佳答案

您需要在复制主键值的实体上对关联进行建模。如果这样做,您可以使用 @MapsId 注释关联。

在您的示例中,Employee 实体的主键是使用 IDENTITY strategy 生成的。 ,并且 Address 实体应使用相同的值。为了实现这一点,您需要对 one-to-one association 进行建模在 Address 实体上。您可以使用双向或单向关联映射。

如果您更喜欢单向注释,则可以使用与 Employee 实体的 address 属性相同的映射注释。当然,之后您需要删除 address 属性;)

在双向映射中,您的 Employee 实体将如下所示:

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

@Id
@Column(name = "EMPLOYEE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "NAME")
private String name;

@Column(name = "SALARY")
private double salary;

@OneToOne
@JoinColumn(name="EMPLOYEE_ID")
private Address address;

public int getId() {
return id;
}

public Employee setId(int id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public Employee setName(String name) {
this.name = name;
return this;
}

public double getSalary() {
return salary;
}

public Employee setSalary(double salary) {
this.salary = salary;
return this;
}

public Address getAddress() {
return address;
}

public Employee setAddress(Address address) {
this.address = address;
return this;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", address=" + address +
'}';
}
}

这是Address 实体。此映射的重要部分是一对一关联上的 @MapsId 注释。

@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {

@Id
@Column(name = "EMPLOYEE_ID")
private int id;

@Column(name = "STREET_NAME")
private String streetName;

@Column(name = "CITY_NAME")
private String cityName;

@Column(name = "STATE_NAME")
private String stateName;

@Column(name = "ZIP_CODE")
private String zipCode;

@OneToOne(mappedBy="address")
@MapsId
private Employee employee

public int getId() {
return id;
}

public Address setId(int id) {
this.id = id;
return this;
}

public String getStreetName() {
return streetName;
}

public Address setStreetName(String streetName) {
this.streetName = streetName;
return this;
}

public String getCityName() {
return cityName;
}

public Address setCityName(String cityName) {
this.cityName = cityName;
return this;
}

public String getStateName() {
return stateName;
}

public Address setStateName(String stateName) {
this.stateName = stateName;
return this;
}

public String getZipCode() {
return zipCode;
}

public Address setZipCode(String zipCode) {
this.zipCode = zipCode;
return this;
}

public Employee getEmployee() {
return employee;
}

public void setEmployee(Employee emp) {
this.employee = emp;
}

@Override
public String toString() {
return "Address{" +
"id=" + id +
", streetName='" + streetName + '\'' +
", cityName='" + cityName + '\'' +
", stateName='" + stateName + '\'' +
", zipCode='" + zipCode + '\'' +
'}';
}
}

如您所见,我删除了 @PrimaryKeyJoinColumn 注释。仅当您的实体映射到 2 个数据库表时才使用它。如果您使用inheritance strategy JOINED,可能会出现这种情况或者如果您使用旧数据库并需要 map 2 tables to the same entity .

关于java - 如何在Hibernate中将主键从主表复制到子表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com