我正在尝试在Spring数据中使用@Embeddable@Embedded。下面是我的方法。

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;

.


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"


现在,当我使用GenertionType策略时,我得到了这个异常。

因此,我的问题是:如何在使用@Embeddable和@Embedded时设置GenerationType策略?

最佳答案

@Embeddable@EmbeddedId通常在JPA / Spring Data中用于复合主键。

如果您有一个简单的生成的主键,则最好将@Id注释与@GenerationType一起使用

08-16 18:49