Eclipse使用mappedBy链接到Hibernate迁移错误

时间:2014-04-16 14:47:23

标签: mysql hibernate postgresql jpa eclipselink

我正在尝试将MySql数据库迁移到Postgresql。我正在使用JPA并使用Eclipse Link作为MySQL数据库,但我正在为Postgresql数据库切换到Hibernate。

以下JPA注释适用于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;

使用Hibernate,我收到以下错误消息:

  

org.hibernate.AnnotationException:标记为mappedBy的关联不得定义数据库映射,如@JoinTable或@JoinColumn

如何让它与Hibernate一起使用?重要的是数据库模式不会更改,因为我想将MySql数据库转储到Postgresql数据库中,而不修改任何表或列名。

干杯,
多米尼克

1 个答案:

答案 0 :(得分:2)

映射意味着Hibernate应该查看/跟踪关系的另一面,因此尝试将连接表移动到关系的另一端:

的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;