0

我正在尝试使用复合键保存实体。key 的元素之一在数据库中具有默认值,因此我不需要手动设置它。我使用@GeneratedValue,但它不工作,因为我看到。Hibernate 仍在尝试插入null值。

例如:

Caused by: org.postgresql.util.PSQLException: 
   ERROR: null value in column "unit_id" violates not-null constraint

Detail: Failing row contains (null, 1, 0, 0, BASIC)

在这一刻我已经完成了@Embeddable所有@EmbeddedId


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Embeddable
public class UnitEntityId implements Serializable {

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "unit_id")
    private Integer unitId;

    @NotNull
    @Column(name = "story_version_id")
    private Integer storyVersionId;
}

-----------------------------------------------

import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "unit", schema = "main_v2")
public class UnitEntity {

    @EmbeddedId
    private UnitEntityId unitEntityId;

    @NotNull
    @Column(name = "point_x")
    private Integer x;

    @NotNull
    @Column(name = "point_y")
    private Integer y;

    @NotNull
    @NotBlank
    @Column(name = "unit_type")
    private String unitType;

}

UnitEntity我试图保存的示例:

var unit = UnitEntity.builder()
                .unitType("BASIC")
                .x(0)
                .y(0)
                .unitEntityId(
                        UnitEntityId.builder()
                                .storyVersionId(1)
                                .build()
                )
                .build();
4

0 回答 0