Reputation: 191
For some reason hibernate generator creates the wrong mappings (annotations). No, there are no errors, and the situation is as follows:
I have a table many-to-many with additional column and I want to put there data (data not recorded and no errors or queries).
Maybe someone know solution? If u dont know read below
I did the same as on the link below (not that I would want to advertise page)
http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ (author of the above page itself notes that the generator is not working as it should).
I did the same thing on, but I have this error
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
Following code
Inventory
@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;
}
}
Any ideas?
Soft: (Hibernate 4.2.15, Spring 3.2.9, Oracle 11g)
Upvotes: 1
Views: 1163
Reputation: 2417
If you look closely at the example you're following (the link), you'll notice that both the association overrides and the mapped-by annotations use a 'dotted name'. This is used to reference fields of a field transitively. In your case, you need to add 'id.' in both places before 'inventory' and 'storageorder'.
Upvotes: 1