I have the following structure in my database: And I need to create entities from my Java application.
I made the following JPA mapping:
@Entity
@Table
public class Chat {
@Id
private long chatId;
@Column(length = 512)
private String title;
//constructors, getters, setters
}
@Embeddable
public class MessagePK implements Serializable {
@Column
private long chatId;
@Column
private int messageId;
//constructors, getters, setters, hashcode and equals
}
@Entity
@Table
public class Message {
@EmbeddedId
private MessagePK messagePK;
@MapsId("chatId")
@JoinColumn(name = "chat_id")
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Chat chat;
@Column(length = 4096)
private String text;
//constructors, getters, setters
}
And it works perfectly. Now I want to add MessageRelation mapping and I want to use entities where it's possible to save all entities by one call to repository. For example:
@Entity
@Table
public class MessageRelation {
@EmbeddedId
private MessagePK messagePK;
@Column
private String type;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private Message target;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private Message source;
//constructors, getters, setters
}
//save operation
MessageRelation relation = new MessageRelation();
relation.setType(...);
relation.setTarget(new Message(...));
relation.setSource(new Message(...));
repository.save(relation);
And it doesn't work, of course. I have tried a hungred of different options of JPA annotation and everytime I was getting some errors. Now I can get working solution only withount Entities parameters in MessageRelation POJO. Please, advice me best mapping which should work with that Database model.
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости