简体   繁体   English

实体之间的JPA eclipselink继承:Oracle数据库

[英]JPA eclipselink Inheritance between entities : oracle database

I'm facing a little problem with my web application which is in vaadin and i'm using jpa and eclipselink for the mapping. 我的Web应用程序在vaadin中遇到了一个小问题,我正在使用jpa和eclipselink进行映射。 I have three entities : 我有三个实体:

                    encaiss (@MappedSuperclass contains just Id)
                       |
                       |
                  Encaissement (it contains the main and common properties)
                     /              \
                    /                \
   Encaissement_Technique          Encaissement_espece

When i create an entity "Encaissement" with "Espece" as type, it is well created in the table Encaissement but it doesn't exist in the table Encaissement_espece. 当我创建类型为“ Espece”的实体“ Encaissement”时,它在表Encaissement中很好地创建,但是在表Encaissement_espece中不存在。

I guess that I should join the two tables according to the identifier (ID) which is in a @MappedSuperclass class. 我想我应该根据@MappedSuperclass类中的标识符(ID)将两个表连接起来。 I would appreciate any help for managing my subordinate class (that is Encaissement_Technique and Encaissement_espece) because my next step would be to add records to those two tables from a simple form (so if i have a field "libelle" which is present in Encaissement but not in Encaissement_Espece how can make such instruction : 对于管理我的下属类(即Encaissement_Technique和Encaissement_espece)的任何帮助,我将不胜感激,因为我的下一步将是从一个简单的表单向这两个表添加记录(因此,如果我有Encaissement中存在的字段“ libelle”,但不在Encaissement_Espece中如何做出这样的指示:

Encaissement_Espece  espece= new Encaissement_Espece();
espece.setLibelle(field.getValue().toString()); 

Those are my entities : 这些是我的实体:

encaiss, this class contain just the Id for all the classes 因此,该类仅包含所有类的ID

@MappedSuperclass
public abstract class encaiss {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="encaiss_seq_gen")
    @SequenceGenerator(name="encaiss_seq_gen", sequenceName="ENCAISSEMENT_SEQ", allocationSize = 1, initialValue = 1)
    protected Integer id_encaissement;

    public Integer getId_encaissement() {
        return id_encaissement;
    }

    public void setId_encaissement(Integer id_encaissement) {
        this.id_encaissement = id_encaissement;
    }
}

Encaissement (wich extend encaiss just to have an Id) Encaissement(为了获得一个ID而将encaiss扩展)

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="ENCAISS_TYPE")
@Table(name="ENCAISSEMENT")
public class Encaissement extends encaiss implements Serializable{

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_CLIENT")
    private Client Client;
@Column(name="ENCAISS_TYPE")
    protected String encaiss_type;
    @Column(name="LIBELLE")
    protected String libelle;
    @Column(name="PIECE_JOINTE")
    protected String piece_jointe;
    @Embedded
    protected Avis_Recette avis_recette;

    public Encaissement(String encaiss_type, String libelle, String piece_jointe){
        this.encaiss_type=encaiss_type;
        this.libelle=libelle;
        this.piece_jointe=piece_jointe;
    }

    public Encaissement(){

    }

}

Encaissement_Espece, inherits from Encaissement Encaissement_Espece,继承于Encaissement

@Entity
@DiscriminatorValue("Espece")
@Table(name="ENCAISSEMENT_ESPECE")
public class Encaissement_Espece extends Encaissement{

    public Caisse getCaisse() {
        return caisse;
    }

    public void setCaisse(Caisse caisse) {
        this.caisse = caisse;
    }

    public float getMontant() {
        return montant;
    }

    public void setMontant(float montant) {
        this.montant = montant;
    }

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_CAISSE")
    private Caisse caisse;

    @Column(name = "MONTANT")
    private float montant;

    public Encaissement_Espece(float montant){
        this.montant=montant;
    }

    public Encaissement_Espece(){

    }

}

Encaissement_Technique, inherits from Encaissement Encaissement_Technique,继承自Encaissement

@Entity
@DiscriminatorValue("Technique")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="ENCAISS_TECHNIQUE_TYPE")
@Table(name="ENCAISSEMENT_TECHNIQUE")
public class Encaissement_Technique extends Encaissement implements Serializable{

    public Banque getBanque() {
        return banque;
    }

    public void setBanque(Banque banque) {
        this.banque = banque;
    }

    public float getPrimeCoass() {
        return primeCoass;
    }

    public void setPrimeCoass(float primeCoass) {
        this.primeCoass = primeCoass;
    }
    public Set<Periode> getPeriode() {
        return periode;
    }

    public void setPeriode(Set<Periode> periode) {
        this.periode = periode;
    }

    public String getEncaiss_technique_type() {
        return encaiss_technique_type;
    }

    public void setEncaiss_technique_type(String encaiss_technique_type) {
        this.encaiss_technique_type = encaiss_technique_type;
    }
@Column(name="PRIMECOASS")
    protected float primeCoass;
    @Column(name="ENCAISS_TECHNIQUE_TYPE")
    protected String encaiss_technique_type;

    public Encaissement_Technique(float primeCoass, String encaiss_technique_type){
        this.primeCoass=primeCoass;
        this.encaiss_technique_type=encaiss_technique_type;
    }

    public Encaissement_Technique(){

    }

}

这是表描述的屏幕截图

I hope i will find a pertinent answer as i searched for this in vain. 我希望我徒劳地寻找这个问题时会找到一个相关的答案。 It'll help me a lot. 这对我有很大帮助。

Thank you. 谢谢。

"When i create an entity "Encaissement" with "Espece" as type, it is well created in the table Encaissement but it doesn't exist in the table Encaissement_espece." “当我创建类型为“ Espece”的实体“ Encaissement”时,在表Encaissement中创建的很好,但是在表Encaissement_espece中不存在。 This statement suggests you have an instance of Encaissement and expect JPA to turn it into an instance of Encaissement_Espece just by changing the encaiss_type value. 该语句建议您有一个Encaissement实例,并希望JPA仅通过更改encaiss_type值就可以将它变成Encaissement_Espece实例。 Java object inheritance doesn't work that way, which is what JPA inheritance tries to map to a relational database. Java对象继承不能以这种方式工作,这就是JPA继承试图映射到关系数据库的方式。 An object in java cannot change what it is simply by setting a flag - you need to create a new instance if you want the data represented differently. Java中的对象无法简单地通过设置标志来更改其含义-如果希望以不同的方式表示数据,则需要创建一个新实例。

In this case, you need to create an instance of the Encaissement_Espece class. 在这种情况下,您需要创建Encaissement_Espece类的实例。 Because this class maps to the Encaissement and Encaissement_espece tables, JPA will automatically insert a row into both to represent this object. 由于此类映射到Encaissement和Encaissement_espece表,因此JPA将自动在两者中插入一行以表示该对象。 When you create Encaissement instance, a row goes into the Encaissement table, while when you create Encaissement_Technique instances, a row goes into both Encaissement_Technique and Encaissement tables. 当您创建Encaissement实例时,一行会进入Encaissement表,而当您创建Encaissement_Technique实例时,一行会同时进入Encaissement_Technique表和Encaissement表。 If you wish to change the object's type once it is persisted, you need to remove the old instance, flush, then persist the new one. 如果要在持久化后更改对象的类型,则需要删除旧实例,刷新,然后持久化新实例。

As mentioned in another answer, the encaiss_type is controlled through the class type itself and so does not need a mapping. 如另一个答案中所述,encaiss_type是通过类类型本身控制的,因此不需要映射。 Having one might be handy for queries or access (though you can just use instance of etc); 拥有一个可能对查询或访问很方便(尽管您可以仅使用etc的实例); it should be marked as insertable=false, updatable=false so that you do not attempt to modify the value directly. 应该将其标记为insertable = false,updatable = false,以便您不要尝试直接修改该值。

remove the 去除

@Column(name="ENCAISS_TYPE")
protected String encaiss_type;

from the Encaissment. 来自Encaissment。

It will be handle automatically by JPA. 它会由JPA自动处理。 It should solve the problem. 它应该解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM