Hibernate应该能够处理重叠的外键吗? [英] Should Hibernate be able to handle overlapping foreign keys?

查看:227
本文介绍了Hibernate应该能够处理重叠的外键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表有两个外键到两个不同的表,有两个外键共享一列

 < codeREATE TABLE ZIPAreas 

country_code CHAR(2)NOT NULL,
zip_code VARCHAR(10)NOT NULL,
state_code VARCHAR(5)NOT NULL,
city_name VARCHAR(100)NOT NULL,
PRIMARY KEY(country_code,zip_code,state_code,city_name),
FOREIGN KEY(country_code,zip_code)参考文献Zips(country_code,code),
FOREIGN KEY(country_code,state_code,city_name)参考城市(country_code,state_code,name)



<正如你所看到的,有两个FKs共享country_code(巧合的是在引导路径末尾引用同一列)。这个实体类看起来像(JPA 1.0 @IdClass):
$ b $ pre $ $ $ c $ @Entity $ b $ @Table(name =ZipAreas )
@IdClass(value = ZipAreaId.class)
public class ZipArea实现Serializable
{
@Id
@Column(name =country_code,insertable = false ,updatable = false)
private String countryCode;

@Id
@Column(name =zip_code,insertable = false,updatable = false)
private String zipCode;

@Id
@Column(name =state_code,insertable = false,updatable = false)
private String stateCode;

@Id
@Column(name =city_name,insertable = false,updatable = false)
private String cityName;

@ManyToOne
@JoinColumns(值= {@JoinColumn(名称= COUNTRY_CODE,referencedColumnName = COUNTRY_CODE),@JoinColumn(名称= ZIP_CODE,referencedColumnName = 代码 )})
private zip zip = null;
$ b @ManyToOne
@JoinColumns(value = {@JoinColumn(name =country_code,referencedColumnName =country_code,insertable = false,updatable = false),@JoinColumn(name = state_code,referencedColumnName =state_code),@JoinColumn(name =city_name,referencedColumnName =name)})
private city city = null;





$ b

正如你所看到的,我将countryCode属性和城市的country_code @JoinColumn标记为只读(insertable = false,updatable = false)。 Hibernate失败,说:

 线程main中的异常javax.persistence.PersistenceException:[PersistenceUnit:geoinfo]无法配置EntityManagerFactory的
在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
处的javax org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
。 (Persistence.java:48)
位于javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
位于tld.geoinfo.Main.main(Main.java:27)在org.hibernate.cfg.Ejb3Column.checkPropertyConsistency(Ejb3Column tld.geoinfo.model.ZipAreacity

引起的:org.hibernate.AnnotationException:在属性混合插入式和非插入列是不允许的。 java:563)
at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2703)
at org.hibernate.cfg.AnnotationBinder.pro cessElementAnnotations(AnnotationBinder.java:1600)
在org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796)
在org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707)
在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3977)
在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processMetadata(Configuration.java:3931)
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
在org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
在org.hibernate.ejb.Ejb3Configuration .buildMappings(Ejb3Configuration.java:1477)
在org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096 )
在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)
... 4 more

老实说,这对我来说很基本。 混合插入和不可插入的列在属性是不允许的是这样一个弱的借口,是不是?

如果Hibernate能够处理这个,例如根据JPA规范?这是一个bug吗?

解决方案

.net / browse / HHH-6221rel =nofollow noreferrer> https://hibernate.atlassian.net/browse/HHH-6221


I have a table that has two foreign keys to two different tables with both foreign keys sharing one column:

CREATE TABLE ZipAreas
(
  country_code CHAR(2) NOT NULL,
  zip_code VARCHAR(10) NOT NULL,
  state_code VARCHAR(5) NOT NULL,
  city_name VARCHAR(100) NOT NULL,
  PRIMARY KEY (country_code, zip_code, state_code, city_name),
  FOREIGN KEY (country_code, zip_code) REFERENCES Zips (country_code, code),
  FOREIGN KEY (country_code, state_code, city_name) REFERENCES Cities (country_code, state_code, name)
)

As you can see, there are two FKs sharing country_code (coincidentally referencing the same column at the end of the referentiation path). The entity class looks like (JPA 1.0 @IdClass):

@Entity
@Table(name = "ZipAreas")
@IdClass(value = ZipAreaId.class)
public class ZipArea implements Serializable
{
    @Id
    @Column(name = "country_code", insertable = false, updatable = false)
    private String countryCode;

    @Id
    @Column(name = "zip_code", insertable = false, updatable = false)
    private String zipCode;

    @Id
    @Column(name = "state_code", insertable = false, updatable = false)
    private String stateCode;

    @Id
    @Column(name = "city_name", insertable = false, updatable = false)
    private String cityName;

    @ManyToOne
    @JoinColumns(value = {@JoinColumn(name = "country_code", referencedColumnName = "country_code"), @JoinColumn(name = "zip_code", referencedColumnName = "code")})
    private Zip zip = null;

    @ManyToOne
    @JoinColumns(value = {@JoinColumn(name = "country_code", referencedColumnName = "country_code", insertable = false, updatable = false), @JoinColumn(name = "state_code", referencedColumnName = "state_code"), @JoinColumn(name = "city_name", referencedColumnName = "name")})
    private City city = null;

    ...
}

As you can see I flagged the countryCode property and city's country_code @JoinColumn as read-only (insertable = false, updatable = false). Hibernate fails with this saying:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: geoinfo] Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at tld.geoinfo.Main.main(Main.java:27)
Caused by: org.hibernate.AnnotationException: Mixing insertable and non insertable columns in a property is not allowed: tld.geoinfo.model.ZipAreacity
    at org.hibernate.cfg.Ejb3Column.checkPropertyConsistency(Ejb3Column.java:563)
    at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2703)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1600)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3977)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3931)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)
    ... 4 more

This looks pretty basic to me honestly. "Mixing insertable and non insertable columns in a property is not allowed" is such a weak "excuse", isn't it?

Should Hibernate be able to handle this, e.g. according to the JPA spec? Is this a bug?

解决方案

Will be supported with Hibernate 5, see https://hibernate.atlassian.net/browse/HHH-6221

这篇关于Hibernate应该能够处理重叠的外键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆