My Entities:
@Entity
public class Document {
@Id
protected String id; //It string in purpose
@OneToOne(cascade = ALL)
@JoinColumn(name = "DOCUMENT_DETAILS")
private DocumentDetails details;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "documentDiscr")
@EqualsAndHashCode
public abstract class DocumentDetails {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Money total;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "SELLER_ID")
private Company seller;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "BUYER_ID")
private Company buyer;
}
@Entity
public class Company {
@Id
protected String id;
private String name;
private String phoneNumber;
private String email;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id")
private Address address;
}
@Entity
@EqualsAndHashCode
public class Address {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Long id;
private String country;
private String city;
private String postalCode;
private String streetName;
private String streetNumber;
private String apartmentNumber;
}
@Path("path")
@Transactional
public class MyResource {
@Inject
MyRepo myRepo;
@PUT
public Document updateDoc(Document document){
myRepo.update(document);
}
}
public class Repo<T extends MyClass> implements MyRepo<T> {
@PersistenceContext
protected EntityManager entityManager;
public T create(T t) {
t.generateId();
this.entityManager.persist(t);
return t;
}
public T update(T entity) {
return entityManager.merge(entity);
}
}
When I call entityManage.update(documentEntity) and same Company is added as supplier and buyer I see
'Multiple representations of the same entity'
.
I read this but nothing helps. When I removed CascadeType.All I am getting
'detached entity passed to persist: my.pckg.Address'
I also tried to remove CascadeType.Merge but error is the same. What I can do? Where is my mistake?
UPDATE
First I changed @ManyToOne(Cascade.All)
to @ManyToOne()
in DocumentDetails
Second I changed @ManyToOne(Cascade.All)
to @ManyToOne(Cascade.Merge)
in DocumentDetails
.
Third I I changed @ManyToOne(Cascade.All)
to @ManyToOne(all types except all and merge)
in DocumentDetails
.
I also tried same with Address
class