Composite Primary key error - Multiple writable mappings exist for the field [message #1104560] |
Sun, 08 September 2013 15:41 |
n n Messages: 2 Registered: September 2013 |
Junior Member |
|
|
hello, have simple example code
@Entity
@Table(name = "item_author_role")
public class ItemAuthorRole implements Serializable {
@Getter @Setter @EmbeddedId
private ItemAuthorRoleId id;
@Setter private Item item;
@MapsId("itemId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id", referencedColumnName = "item_id")
public Item getItem() { return item; }
@Override
public boolean equals(Object obj) {
if(obj instanceof ItemAuthorRole) {
ItemAuthorRole role = (ItemAuthorRole) obj;
return role.getItem().equals(item);
}
return false;
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31).append(item).toHashCode();
}
}
@Embeddable
class ItemAuthorRoleId implements Serializable {
@Getter @Setter @Column(name = "item_id", nullable = false)
private int itemId;
@Override
public boolean equals(Object obj) {
if(obj instanceof ItemAuthorRoleId) {
ItemAuthorRoleId id = (ItemAuthorRoleId) obj;
return id.itemId == itemId;
}
return false;
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31).append(itemId).toHashCode();
}
}
Item
@Entity
@Table(name = "items")
public class Item extends AbstractModel implements Serializable {
@Setter private Set<ItemAuthorRole> itemAuthorRoleSet;
@Setter private Technic technic;
{
itemAuthorRoleSet = new HashSet<>(0);
}
@Override
@SequenceGenerator(name = "isg", sequenceName = "item_sequence", initialValue = 1, allocationSize = 2)
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "isg")
public Integer getId() { return super.getId(); }
@JoinColumn(name = "item_id", insertable = false, updatable = false) //no effect
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH}, mappedBy = "item", targetEntity = ItemAuthorRole.class)
public Set<ItemAuthorRole> getItemAuthorRoleSet() { return itemAuthorRoleSet; }
@OneToOne
@JoinColumn(name = "technic_id")
public Technic getTechnic() { return technic; }
@Version
@Column(name = "opt_lock")
private Long version;
}
Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [item_author_role.item_id]. Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[item]
Descriptor: RelationalDescriptor(model.item.ItemAuthorRole --> [DatabaseTable(item_author_role)])
i seen this link http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/mappedbyid
but i don`t understand what wrong in my example, thanks
[Updated on: Mon, 09 September 2013 03:28] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04327 seconds