gpt4 book ai didi

java - JPA 2 - 如何使用 Spring Data JPA 构建具有主键也是外键的实体?

转载 作者:行者123 更新时间:2023-12-01 14:28:57 26 4
gpt4 key购买 nike

鉴于以下表格:

Car
int id PK
int modelId FK

CarDetails
int carId PK, FK to Car.id
varchar(50) description

我将如何表明 @IdCarDetails也是 Car 的外键?

我试过了:
@Entity
public class Car {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@ManyToOne
@JoinColumn(name = "modelId", nullable = false)
private Model model;

//setters & getters
}

@Entity
public class CarDetails {

@Id
@OneToOne
@JoinColumn(name = "carId", nullable = false)
private Car car;

private String description;

//setters & getters
}

但是,我收到错误
org.hibernate.MappingException: Composite-id class must implement Serializable: com.example.CarDetails
实现后 Serializable我收到 This class [class com.example.CarDetails] does not define an IdClass .但是在添加 @IdClass(Car.class) 后我仍然收到错误消息到 CarDetails类(class)。

更新
IdClass错误源自 Spring: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDetailsRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class com.example.CarDetails] does not define an IdClass
这是 CarDetailsRepository:
public interface CarDetailsRepository extends JpaRepository<CarDetails, Car> {

}

以下是我的 gradle 构建文件的相关部分:
plugins {
id 'java'
id 'eclipse'
id 'maven-publish'
id 'io.spring.dependency-management' version '1.0.3.RELEASE'
}

repositories {
mavenCentral()
mavenLocal()
}


dependencies {

pmd group: 'org.hibernate', name: 'hibernate-tools', version: '5.2.3.Final'
pmd group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'
pmd group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'

compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.hibernate:hibernate-validator:5.2.4.Final')
compile('org.hibernate:hibernate-envers:5.2.10.Final')
compile('org.hibernate:hibernate-core:5.2.10.Final')
compile('org.hibernate.common:hibernate-commons-annotations:5.0.1.Final')
runtime('net.sourceforge.jtds:jtds:1.3.1')
runtime('com.microsoft.sqlserver:sqljdbc:4.2')
runtime('javax.el:javax.el-api:2.2.4')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
imports { mavenBom('org.springframework.boot:spring-boot-dependencies:1.5.4.RELEASE') }
}

最佳答案

可能最好的方法是引用carId在您的 CarDetails 中两次实体——一次用于@Id,一次用于外键引用。您必须使用 insertable=false 声明这些引用之一。和 updatable=false这样 JPA 就不会在尝试在两个位置管理同一列时感到困惑:

@Entity
public class CarDetails {

@Id
@Column(name = "carId", insertable = false, updatable = false)
private int carId; // don't bother with getter/setter since the `car` reference handles everything

@OneToOne
@JoinColumn(name = "carId", nullable = false)
private Car car;

private String description;

//setters & getters
}

它看起来很奇怪,但它会起作用并且它实际上是(最)首选的方法。

关于java - JPA 2 - 如何使用 Spring Data JPA 构建具有主键也是外键的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786137/

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