English 中文(简体)
JPA 2 - 仅包括一个综合主要关键领域的外在钥匙?
原标题:JPA 2 - Foreign key that only includes one field from a composite primary key?

在JPA 2/Hibernate工作的大型和外国钥匙混杂在一起,我遇到麻烦。 我试图在各国和各省建立一个简单的设想:

国家实体:

@Entity
@Table(name = "country")
public class Country extends DomainObjectBase implements Serializable {

    @Id
    @Basic(optional = false)
    @Column(name = "code")
    private String code;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    private List<Province> provinces;
}

小学 关键:

@Embeddable
public class ProvincePK implements Serializable {

    @Basic(optional = false)
    @Column(name = "code")
    private String code;

    @Basic(optional = false)
    @Column(name = "country_code")
    private String countryCode;
}

省实体:

@Entity
@Table(name = "province")
public class Province extends DomainObjectBase implements Serializable {

    @EmbeddedId
    protected ProvincePK provincePK;

    @MapsId("country_code")
    @JoinColumn(name = "country_code", referencedColumnName = "code", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Country country;
}

这为我设立了正确的表格,但有一个例外:

国家表:

  1. code PK
  2. ...

各省

  1. code PK FK - This is where the problem is its creating a foreign key reference to the country table s code column
  2. country_code FK This is the only foreign key reference I want
  3. ...

我如何描绘我的实体/组合关键人物,以便产生我想要的图象? 现在,我可以把任何数据输入该省,因为我国期望该省包含《省法典》。

Thanks for any help.

问题回答

引证。 我发现,当我采用类似数据模型时,我会为我工作。

@Entity
@Table(name = "province")
@IdClass(ProvincePK.class)
public class Province extends DomainObjectBase implements Serializable {
        
    @Id
    @Basic(optional = false)
    @Column(name = "code")
    private String code;
    
    @Id
    @Basic(optional = false, insertable = false, updatable = false)
    @Column(name = "country_code")
    private String countryCode;
            
    @JoinColumn(name = "country_code", referencedColumnName = "code")
    @ManyToOne
    private Country country;
}

地图ID论点必须与<代码>中的属性名称相符。 省PK 页: 1 标识应为insertable=true,updatable=true,然后运行。 这里是法典——

@Entity
@Table(name = "province")
public class Province  implements Serializable {

    @EmbeddedId
    protected ProvincePK provincePK;

    @MapsId(value = "country_code")
    @JoinColumn(name = "country_code", referencedColumnName = "code")
    @ManyToOne(optional = false)
    private Country country;
}

@Embeddable
public class ProvincePK implements Serializable {

    @Basic(optional = false)
    @Column(name = "code")
    private String code;

    @Basic(optional = false)
    @Column(name = "country_code")
    private String country_code;
}

@Entity
@Table(name = "country")
public class Country  implements Serializable {

    @Id
    @Basic(optional = false)
    @Column(name = "code")
    private String code;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    private List<Province> provinces;
}

希望会有所助益。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签