gpt4 book ai didi

java - “基本”属性类型不应为持久性实体

转载 作者:行者123 更新时间:2023-11-30 12:01:55 31 4
gpt4 key购买 nike

我在另一个实体类中引用一个实体并收到此错误。下面是示例代码。我在 persistence.xml 中也有这些类。

是什么导致了这个问题?我正在使用 Spring 数据 JPA 和 Hibernate。

import javax.persistence.*;
@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
private long id;

@JoinColumn(name = "address_id", nullable = false)
private Address address;

@Id
@Column(name = "id")
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}
import javax.persistence.*;
@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
private long id;
private String street;

@Id
@Column(name = "id")
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

@Basic
@Column(name = "street")
public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}
}

最佳答案

那么请尝试以下操作:

@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
private Long id;

private AddressEntity address;

@Id
@Column(name = "id")
public Long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

@OneToOne
@JoinColumn(name = "address_id", nullable = false)
public AddressEntity getAddress() {
return address;
}

public void setAddress(AddressEntity address) {
this.address = address;
}
}


@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
private Long id;
private String street;

@Id
@Column(name = "id")
public Long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}


@Column(name = "street")
public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}
}

基本上我所做的就是将 long 替换为 Long。并添加了@OneToOne我删除了 @Basic 因为它是可选的。我相信它应该可以正常工作

关于java - “基本”属性类型不应为持久性实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59162862/

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