8

I'm trying to migrate a MySql database to Postgresql. I am using JPA and was using Eclipse Link for the MySQL database, but I am switching to Hibernate for the Postgresql database.

The following JPA annotations work with EclipseLink:

UserBean.java:

@OneToMany(mappedBy = "sender", cascade = CascadeType.ALL)
@JoinTable(name = "MESSAGES_SENT")
private List<MessageBean> messagesSent;

@OneToMany(mappedBy = "receiver", cascade = CascadeType.ALL)
@JoinTable(name = "MESSAGES_RECEIVED")
private List<MessageBean> messagesReceived;

MessageBean.java:

@ManyToOne
private UserBean sender;

@ManyToOne
private UserBean receiver;

With Hibernate, I get the following error message:

org.hibernate.AnnotationException: Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn

How can I get it to work with Hibernate? It is important that the database schema does not change, because I want to dump the MySql database into the Postgresql database, without modifying any tables or column names.

Cheers,
Dominik

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
user473453
  • 894
  • 2
  • 11
  • 23
  • I've posted an answer, you can also my answer (second on the list) to this other question -> http://stackoverflow.com/questions/2749689/what-is-the-owning-side-in-an-orm-mapping – Angular University Apr 16 '14 at 22:31

1 Answers1

7

Mapped by means that Hibernate should look/track the other side of the relation, so try to move the joined table to the other side of the relation:

UserBean:

@OneToMany(mappedBy = "sender", cascade = CascadeType.ALL)
private List<MessageBean> messagesSent;

@OneToMany(mappedBy = "receiver", cascade = CascadeType.ALL)
private List<MessageBean> messagesReceived;

MessageBean:

@ManyToOne
@JoinTable(name = "MESSAGES_SENT")
private UserBean sender;

@ManyToOne
@JoinTable(name = "MESSAGES_RECEIVED")
private UserBean receiver;
Angular University
  • 42,341
  • 15
  • 74
  • 81