【问题标题】:Problems with Hibernate MappingHibernate 映射的问题
【发布时间】:2021-10-24 06:43:14
【问题描述】:

我在使用复合键映射两个类时遇到问题。

第一类是Product:

@Entity
@Table(name = "Products")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class Product {
    @EmbeddedId
    private ProductKey prodPK;
    @Column(name = "name", length = 50, nullable = false)
    private String name;
    @Column(name = "description", length = 80)
    private String description;
    @Column(name = "totalStock", columnDefinition = "double(8,2) default 0")
    private double totalStock;
    @ManyToOne
    @JoinColumn(name = "companyId", referencedColumnName = "id", nullable = false)
    private Company company;
}

有了这个@EmbeddedId:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Embeddable
public class ProductKey implements Serializable {
    @Column(name = "sku", length = 50)
    private String sku;
    @Embedded
    private LotKey lot;
}

同时,这个嵌入类有另一个复合键“LotKey”作为其复合键的一部分

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Embeddable
public class LotKey implements Serializable {
    @Column(name = "lot")
    private String lot;
    @ManyToOne
    @JoinColumn(name = "company", referencedColumnName = "id")
    private Company company;
}

属于哪个类:

@Entity
@Table(name = "Lots")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class Lote {
    @EmbeddedId
    private LotKey lotpk;
    @Column(name = "stock")
    private double stock;
    @Column(name = "expirationDate", columnDefinition = "default current_timestamp()")
    private Date expirationDate;
}

但我无法引用它们:

@Entity
@Table(name = "quantityProduct")
public class QuantityProduct{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @ManyToOne
    @JoinColumns({
            @JoinColumn(
                    name = "sku",
                    referencedColumnName = "sku"),
            @JoinColumn(
                    name = "lot")
    })
    private Product product;
    @Column(name = "quantity", columnDefinition = "double(8,2) default 0")
    private double quantity;
}

我收到以下错误

image

非常感谢!

【问题讨论】:

    标签: java spring hibernate jpa annotations


    【解决方案1】:

    QuantityProduct 中,同时设置referencedColumnName

     @JoinColumn(
                    name = "lot")
    

    【讨论】:

    • 感谢您的回答,我试过了,但如果我这样做,会在线程“main”org.hibernate.AnnotationException 中引发以下异常异常:uy.com.storagemodule 的referencedColumnNames(sku,lote) .logic.ProductoCantidad.producto 引用 uy.com.storagemodule.logic.Producto 未映射到 org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:333) 处的单个属性。 . .
    • lote? ProductKey中不是lot吗?
    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2016-08-07
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多