Java Spring数据:如何在使用@Embeddeble和@Embedded时设置GenerationType策略
我试图在Spring数据中使用Java Spring数据:如何在使用@Embeddeble和@Embedded时设置GenerationType策略,java,hibernate,spring-boot,spring-data-jpa,Java,Hibernate,Spring Boot,Spring Data Jpa,我试图在Spring数据中使用@Embedded和@Embedded。下面是我的方法 设备实体: @Entity @Table(name = "device") public class DeviceEntity implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId @AttributeOverrides({@AttributeOver
@Embedded
和@Embedded
。下面是我的方法
设备实体:
@Entity
@Table(name = "device")
public class DeviceEntity implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "id" , column= @Column(name = "id",nullable = false))})
private DeviceIdType deviceIdType;
.
@Embeddable
public class DeviceIdType implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
"code": "4715",
"message": "Something went wrong",
"additionalInfo": "null id generated for:class com.b.DeviceEntity; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.b.DeviceEntity"
设备类型:
@Entity
@Table(name = "device")
public class DeviceEntity implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "id" , column= @Column(name = "id",nullable = false))})
private DeviceIdType deviceIdType;
.
@Embeddable
public class DeviceIdType implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
"code": "4715",
"message": "Something went wrong",
"additionalInfo": "null id generated for:class com.b.DeviceEntity; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.b.DeviceEntity"
。
.
错误消息:
@Entity
@Table(name = "device")
public class DeviceEntity implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "id" , column= @Column(name = "id",nullable = false))})
private DeviceIdType deviceIdType;
.
@Embeddable
public class DeviceIdType implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
"code": "4715",
"message": "Something went wrong",
"additionalInfo": "null id generated for:class com.b.DeviceEntity; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.b.DeviceEntity"
现在,当我使用GenerationType策略时,我得到了这个异常
因此,我的问题是:如何在使用@Embedded和@Embedded时设置GenerationType策略?
@Embedded
和@EmbeddedId
在JPA/Spring数据中大部分时间用于复合主键
如果您有一个简单生成的主键,最好将
@Id
注释与@GenerationType
一起使用,检查这是否有助于->@Id对嵌入对象(值类型)不起作用。它只适用于实体。没错,但在他的例子中,他只嵌入了一个long,可以在主实体类中设置,如果你这样看,那么是的。id可能只是DeviceEntity的一部分,而不是一个可嵌入的对象,但这完全取决于他的设计,也就是说,他是否打算使用其他需要更好OOP方法的字段。