问题描述
我有一个名为"EventCheckin"的模型,该模型具有一个映射到事件"和用户"的ManyToOne映射. "EventCheckin"表的PrimaryKey是用户的ID和事件的ID.我试图在EventCheckin模型中使用"EmbeddedId"来表示这一点,但是当我尝试保存EventCheckin时,它试图将user_id和event_id值两次放入表中,这显然失败了:
I have a model called "EventCheckin" which has a ManyToOne mapping to an "Event" and a "User". The PrimaryKey of the "EventCheckin" table is the id of the user and the id of the event. I'm trying to represent this using an "EmbeddedId" in my EventCheckin model but when I attempt to save an EventCheckin it tries to put the user_id and event_id values into the table twice which obviously fails:
Caused by: org.h2.jdbc.JdbcSQLException: Duplicate column name "USER_ID"; SQL statement:
insert into eventCheckin (event_id, user_id, latitude, longitude, user_id, event
_id) values (?,?,?,?,?,?) [42121-158]
EventCheckin类:
EventCheckin class:
@Entity
@Table(name="eventCheckin")
public class EventCheckin extends Model
{
@EmbeddedId public CheckinId id;
@MapsId("userId")
@JoinColumn(name="user_id")
@ManyToOne public User user;
@MapsId("eventId")
@JoinColumn(name="event_id")
@ManyToOne public Event event;
.....
}
CheckinId EmbeddedId类::
CheckinId EmbeddedId class::
@Embeddable
public class CheckinId implements Serializable
{
public Long eventId;
public String userId;
.....
}
我的EventCheckin数据库表定义如下:
And my database table for EventCheckin is defined as this:
create table eventCheckin (
user_id varchar(255) not null,
event_id bigint not null,
latitude float,
longitude float,
constraint pk_eventCheckIn primary key (user_id,event_id),
foreign key (user_id) references user (email),
foreign key (event_id) references event (id)
);
推荐答案
似乎您尝试通过@MapsId和@EmbeddedId做同样的事情.一个(有效的)选择是使用IdClass(等于,哈希码,多余的属性等被删除):
It looks like you try to do same thing via @MapsId and @EmbeddedId. One (working) option is to go for IdClass (equals, hashcode, extra attributes etc are cut away):
@Entity
public class User {
@Id public String id;
}
@Entity
public class Event {
@Id public long id;
}
public class CheckinId implements Serializable {
public Long event;
public String user;
}
@Entity
@IdClass(CheckinId.class)
@Table(name="eventCheckin")
public class EventCheckin {
@Id
@JoinColumn(name="user_id")
@ManyToOne public User user;
@Id
@JoinColumn(name="event_id")
@ManyToOne public Event event;
}
这篇关于将EmbeddedId与Ebean一起使用ManyToOne映射时出现重复的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!