如何在List <items>?</items>上使用UniqueContraints

时间:2013-03-06 20:22:56

标签: java java-ee jpa

我的实体看起来像

@Entity

    @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"id", "service_version_id"}))
    public class MarketVersion extends AbstractVersion<MarketVersion> {

        @ManyToOne(cascade = CascadeType.ALL)
        private Market market;

        @OneToOne
        private MarketVersion parentVersion;

        @OneToMany(cascade = CascadeType.MERGE)
        @JoinColumn(name = "services_id", referencedColumnName = "id")
        private List<ServiceVersion> services;
}

和服务看起来像

public class ServiceVersion extends AbstractVersion<ServiceVersion> {

    @ManyToOne(cascade = CascadeType.ALL)
    private Service service;
    @OneToOne
    private ServiceVersion parentVersion;
    @Column
    private String description;
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private ServiceImplementations implementation;
}

当我运行时,我看到了

[hibernatetool] javax.persistence.PersistenceException: [PersistenceUnit: market] Unable to configure EntityManagerFactory
[hibernatetool] org.hibernate.AnnotationException: Unable to create unique key constraint (id, service_version_id) on table MarketVersion: service_version_id not found

我确信自己是新手,我没有做正确的事,但却无法理解,做什么 请指教

我想要一个对unique(market_version_id和service_version_id)有约束的新表

1 个答案:

答案 0 :(得分:0)

您的约束中的列名为service_version_id,而您在实体中的加入列已加入列services_id。 要么您需要在数据库中创建列service_version_id,要么将约束列重命名为services_id,具体取决于您要实现的目标。