1

必须有两个表 Customer 和 Upload,我在其中跟踪客户上传的项目数量。它与共享主键是一对一的关系,共享主键CustomerID是唯一标识符。我正在努力让保存工作。我正在使用 Hibernates uuid2 策略来处理唯一标识符的创建/保存。在我包含上传实体之前,客户将使用 uuid 正确保存。基本上,我希望在有人制作客户对象时创建上传。

顾客:

@Entity
public class Upload implements Serializable {
    @Column(name="UPLOADCOUNT",    nullable=true,  unique=false)
    private Integer uploadCount = new Integer(0); 

    @MapsId ("CUSTOMERID")
    @OneToOne (optional=false)
    @JoinColumn(name="CUSTOMERID", referencedColumnName = "CUSTOMERID", nullable=false,  unique=true ) 
    private Customer customer;  

    @Id 
    @Column(name="CUSTOMERID", length=36, nullable=false, unique=true, insertable=false, updatable=false)
    private String customerId_;

     public Upload(Customer customer) {    
         this.customer = customer;
     }
}

上传:

@Entity
public class Customer implements Serializable {


    @Id
    @Column(name = "CUSTOMERID", unique = true)
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String customerId;

    @OneToOne(mappedBy = "customer", optional = false, cascade = CascadeType.ALL)
    private Upload upload ; 


    public Customer() {
        this.upload = new Upload(this);        
    }
}

要保存的代码:

Customer customer = new Customer();
EntityManager em = EntityManagerUtil.getEntityManager();
EntityTransaction trans = em.getTransaction();

trans.begin();
em.persist(customer);
trans.commit();

例外:

 javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Upload
4

2 回答 2

2

而不是@JoinColumn你可以试试这个:

@PrimaryKeyJoinColumn(name="CUSTOMERID", referencedColumnName="CUSTOMERID")

更多细节在这里

或另一种可能的解决方案:像这样更新您的Upload课程。

@Id 
@Column(name="CUSTOMERID", length=36, nullable=false, unique=true, insertable=false, updatable=false)
@GeneratedValue(generator=”foreign”) 
@GenericGenerator(name=”foreign”, strategy = “foreign”, parameters =    {@Parameter(name=”property”, value=”customer”)})
private String customerId_;
于 2013-01-11T20:24:43.267 回答
0

创建自生成 UUID 相关实体的重要一点是将 UUID 设置为 null 值。

如果我们对 uuid 进行硬编码,例如,一个 UUID.randomUUID (),我们将得到一个类似于以下的 fk 错误PSQLException: null value in column "second_table_uuid" violates not-null constraint

创建对象时,请确保相关实体的构造函数的默认值为 null。如果它是包含 B 的实体 A,则只需要 B 的 uuid 中的空值,而不是 A 的空值。但以防万一,除非您已经知道 fk,否则您可以将空值放入所有 UUID。

另一个重要的事情是使用@JoinColumn而不是@PrimaryKeyJoinColumn,这也会产生错误。

我附上了我制作的代码片段。

@Entity
@Table(name = "parent")
data class Parent(
        @Id
        @GeneratedValue
        @Column(name = "parent_uuid")
        val parentUUID: UUID? = null,

        @OneToOne(cascade = [CascadeType.ALL])
        @JoinColumn(name = "child_uuid", referencedColumnName = "child_uuid")
        var child: Child
) {
        constructor() : this(child = Child())
}

@Entity
@Table(name = "child")
data class Child(
        @Id 
        @GeneratedValue 
        @Column(name = "child_uuid") 
        val childUUID: UUID? = null,
    
        @Column(name = "name") 
        val name: String,
) {
        constructor() : this(name = "")
}

并且组合将在 UUID 中留下隐含的空值。

val child = Child(name = "test")
val parent = Parent(child = child)

总帐

于 2020-09-28T06:26:00.237 回答