2

我想从包含约会表和约会和房间连接表的现有数据库中导入数据。

TABLE Appointment {
    id
    ...
}

TABLE Appointment_Room {
    appointment_id,
    room_id
}

我无权访问 Room 表。

对于我的申请,我有以下约会实体:

@Entity
public class Appointment {
    private int id;
    ...
    private List<Integer> roomIdList;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    ...

    @JoinColumn (
        table = "Appointment_Room",
        name = "appointment_id",
        referencedColumnName = "id"
    )
    public List<Integer> getRoomIdList() {
        return roomIdList;
    }
    public void setRoomIdList(List<Integer> roomIdList) {
        this.roomIdList = roomIdList;
    }
}

由于我只需要与约会关联的房间的外键值,因此我希望 Appointment 的实例包含这些外键的列表。

但是现在我收到以下错误消息:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Appointment_Room, for columns: [org.hibernate.mapping.Column(roomIdList)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292)
    at org.hibernate.mapping.Property.isValid(Property.java:239)
    ...

我真的不明白是什么导致了这里的问题,也许有人知道解决方案?

也许使用 ORM 框架不是这种场景的正确方法,可能还有其他解决方案,但问题似乎很简单,我很好奇是否有可能将此 ManyToOne 关系映射到外键列表.

4

1 回答 1

2

问题是您忘记使用 注释您的getRoomIdList()方法@ElementCollection,并且 JoinColumn 不是用于描述必须使用哪些表和列的适当注释。

这是一个显示 hos 要做的例子。

@Entity
public class User {
   [...]
   public String getLastname() { ...}

   @ElementCollection
   @CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
   @Column(name="nickname")
   public Set<String> getNicknames() { ... } 
}
于 2014-08-02T21:14:09.990 回答