使用带有Ebean的ManyToOne映射的EmbeddedId时重复列

时间:2012-03-26 18:03:39

标签: jpa playframework playframework-2.0 ebean composite-key

我有一个名为“EventCheckin”的模型,它具有到“事件”和“用户”的ManyToOne映射。 “EventCheckin”表的PrimaryKey是用户的id和事件的id。我试图在我的EventCheckin模型中使用“EmbeddedId”来表示这一点,但是当我尝试保存EventCheckin时,它会尝试将user_id和event_id值放入表中两次,这显然会失败:

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类:

@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 class ::

@Embeddable 
public class CheckinId implements Serializable
{
    public Long eventId;  
    public String userId;
    .....
}

我的EventCheckin数据库表定义如下:

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

2 个答案:

答案 0 :(得分:2)

看起来你试图通过@MapsId和@EmbeddedId做同样的事情。一个(工作)选项是去IdClass(等于,哈希码,额外属性等被删除):

    @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;
    }

答案 1 :(得分:0)

发生此错误是因为复合键使用了列user_idevent_id,同时@ManyToOne映射使用了列insertable = false, updatable = false@Joincolumn。解决方案是将@Entity @Table(name="eventCheckin") public class EventCheckin extends Model { public EventCheckin() { id = new CheckinId(); } @EmbeddedId public CheckinId id; @ManyToOne @MapsId("userId") @JoinColumn(name="user_id", insertable = false, updatable = false) private User user; @ManyToOne @MapsId("eventId") @JoinColumn(name="event_id", insertable = false, updatable = false) private Event event; public void setUser(User aUser) { user = aUser; id.userId = aUser.getId(); } public void setEvent(Event aEvent) { event = aEvent; id.eventId = aEvent.getId(); } } 属性添加到@Embeddable public class CheckinId implements Serializable { public Long eventId; public String userId; @Override public int hashCode() { return eventId.intValue() + userId.length(); } @Override public boolean equals(Object obj) { if (this == obj) return true; CheckinId c = (CheckinId)obj; if(c==null) return false; if (c.eventId.equals(eventId) && c.userId.equals(userId)) { return true; } return false; } } 注释中。以下是此解决方案的工作代码:

EventChekin.java:

{{1}}

Checkin.java

{{1}}