0

我有两个实体,它们之间有关系。这是我的第一个实体:

    @Entity
    @Table(name = "APPLICATION_DEVICE")
    public class ApplicationDevice implements Serializable {
    [...]
    @Id
        @ManyToOne
        @JoinColumn(name = "udid", nullable=false)
        public Device getDevice()
        {
            return device;
        }

        public void setDevice(Device device)
        {
            this.device = device;
        }

        @Id
        public Long getApplicationId()
        {
            return applicationId;
        }

        public void setApplicationId(Long applicationId)
        {
            this.applicationId = applicationId;
        }
[...]

请注意,它有一个包含两个字段的主键:applicationId 和一个设备(它是另一个关系)。

我的另一个实体是:

@Entity
@Table(name = "SEGMENT_APPLICATION_DEVICES")
public class SegmentApplicationDevice
{
[...]

@Id
    @GeneratedValue
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    @ManyToOne
    public ApplicationDevice getApplicationDevice()
    {
        return applicationDevice;
    }

    public void setApplicationDevice(ApplicationDevice applicationDevice)
    {
        this.applicationDevice = applicationDevice;
    }

[...]

这是hibernate尝试映射此实体时的例外:

Exception constructing service 'HibernateSessionSource': Error invoking service builder method org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger, List, RegistryShutdownHub) (at HibernateCoreModule.java:123) (for service 'HibernateSessionSource'): Foreign key (FK5B8CB9821C11FEAE:SEGMENT_APPLICATION_DEVICES [applicationDevice_applicationId])) must have same number of columns as the referenced primary key (APPLICATION_DEVICE [udid,applicationId])

我在做什么错?谢谢!

4

1 回答 1

1

ApplicationDevice 有一个由 2 列组成的复合 id。但是,ApplicationDevice 的 manytoone 没有指定列(应该有 2 个),因此使用带有“entityname_id”的一列的默认值。

为了缓解这种情况,请在多对一中指定列,它们是 ApplicationDevice 的连接列。

于 2012-07-10T11:53:28.793 回答