mappedBy引用一个未知的目标实体属性,Hibernate注释

时间:2014-08-25 23:02:43

标签: hibernate

由于某种原因,hibernate生成器会创建错误的映射(注释)。不,没有错误,情况如下:

我有一个多对多的表,附加列,我想放置数据(数据没有记录,没有错误或查询)。

也许有人知道解决方案?如果你不知道在下面阅读


我做的与下面的链接相同(不是我想要宣传页面)

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ (上一页的作者本身注意到生成器不能正常工作)。

我做了同样的事情,但我有这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/hibernate.cfg.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/hibernate.cfg.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.dms.entity.InventoryOrder.inventory in com.dms.entity.Inventory.inventoryOrders

以下代码

广告

@Entity
@Table(name = "INVENTORY")
public class Inventory implements java.io.Serializable {

    private long id;
    private String symbol;
    private Set<InventoryOrder> inventoryOrders = new HashSet<InventoryOrder>(0);


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mysequence")
    @SequenceGenerator(name = "seq", mysequence= "SEQ_INV")
    @Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "SYMBOL", length = 32)
    public String getSymbol() {
        return this.symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "inventory")
    public Set<InventoryOrder> getInventoryOrders() {
        return this.inventoryOrders;
    }

    public void setInventoryOrders(Set<InventoryOrder> inventoryOrders) {
        this.inventoryOrders = inventoryOrders;
    }

}

StorageOrder

@Entity
@Table(name = "STORAGEORDER")
public class Storageorder implements java.io.Serializable {

    private long id;
    private String code;
    private Set<InventoryOrder> inventoryOrders = new HashSet<InventoryOrder>(0);


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mysequence")
    @SequenceGenerator(name = "mysequence", sequenceName = "SEQ_STO")
    @Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "CODE", length = 32)
    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "storageorder")
    public Set<InventoryOrder> getInventoryOrders() {
        return this.inventoryOrders;
    }

    public void setInventoryOrders(Set<InventoryOrder> inventoryOrders) {
        this.inventoryOrders = inventoryOrders;
    }

}

InventoryOrder

@Entity
@Table(name = "INVENTORY_ORDER")
@AssociationOverrides({
    @AssociationOverride(name = "storageorder", 
        joinColumns = @JoinColumn(name = "STORAGEORDERID")), <-- I have tried here put "ID", but same error
    @AssociationOverride(name = "inventory", 
        joinColumns = @JoinColumn(name = "INVENTORYID")) }) <-- I have tried here put "ID", but same error
public class InventoryOrder implements java.io.Serializable {

    private InventoryOrderId id = new InventoryOrderId();
    private Long quantity;

    @EmbeddedId
    public InventoryOrderId getId() {
        return this.id;
    }

    public void setId(InventoryOrderId id) {
        this.id = id;
    }

    @Transient
    public Storageorder getStorageorder() {
        return getId().getStorageorder();
    }

    public void setStorageorder(Storageorder storageorder) {
        getId().setStorageorder(storageorder);
    }

    @Transient
    public Inventory getInventory() {
        return getId().getInventory();
    }

    public void setInventory(Inventory inventory) {
        getId().setInventory(inventory);
    }

    @Column(name = "QUANTITY", precision = 10, scale = 0)
    public Long getQuantity() {
        return this.quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }

    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        InventoryOrder that = (InventoryOrder) o;

        if (getId() != null ? !getId().equals(that.getId())
                : that.getId() != null)
            return false;

        return true;
    }

    public int hashCode() {
        return (getId() != null ? getId().hashCode() : 0);
    }

}

InventoryOrderId

@Embeddable
public class InventoryOrderId implements java.io.Serializable {

    private Storageorder storageorder;
    private Inventory inventory;

    @ManyToOne
    public Storageorder getStorageorder() {
        return this.storageorder;
    }

    public void setStorageorder(Storageorder storageorder) {
        this.storageorder = storageorder;
    }

    @ManyToOne
    public Inventory getInventory() {
        return this.inventory;
    }

    public void setInventory(Inventory inventory) {
        this.inventory = inventory;
    }

    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        InventoryOrderId that = (InventoryOrderId) o;

        if (storageorder != null ? !storageorder.equals(that.storageorder)
                : that.storageorder != null)
            return false;
        if (inventory != null ? !inventory.equals(that.inventory)
                : that.inventory != null)
            return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (storageorder != null ? storageorder.hashCode() : 0);
        result = 31 * result + (inventory != null ? inventory.hashCode() : 0);
        return result;
    }

}

有什么想法吗?

Soft :( Hibernate 4.2.15,Spring 3.2.9,Oracle 11g)

1 个答案:

答案 0 :(得分:0)

如果仔细查看您正在关注的示例(链接),您会注意到关联覆盖和映射注释都使用“点名”。这用于传递地引用字段的字段。在您的情况下,您需要添加“id”。在'inventory'和'storageorder'之前的两个地方。