Id generator in jpa (eclipselink) inheritance return random values

安稳与你 提交于 2019-12-12 04:55:45

问题


I'm using eclipselink in my web application (i use vaadin with oracle as DATABASE) but i'm facing a problem with the Id values.

I have this simple schema :

   encaiss (@MappedSuperclass contains just Id)
                             |
                             |
       Encaissement (it extends encaiss so as to have the ID)
                 /                                   \
                /                                     \
Encaissement_Technique (extends Encaissement)          Encaissement_espece
       /                \
      /                  \
Encaissement_Cheque      Encaissement_Virement (both extend Encaissement_Technique to have the ID)

When i create an instance of Encaissement_virement for example, the Id generated is random eventhough i set allocationSize = 1, initialValue = 1.

Those are the entities :

====================Entity encaiss (contains just the 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 long id_encaissement;

    public long getId_encaissement() {
        return id_encaissement;
    }

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

}

============================entity Encaissement=======================

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

@Column(name="LIBELLE")
    protected String libelle;
    @Column(name="PIECE_JOINTE")
    protected String piece_jointe;
    @Temporal(TemporalType.DATE)
    @Column(name="DATE_ENCAISSEMENT")
    protected Date date_encaissement;
    @Embedded
    protected Avis_Recette avis_recette;

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

    public Encaissement(){

    }

}

====================entity Encaissement_Technique======================

@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 Encaissement_Technique(){

    }

}

====================entity Encaissement_Cheque========================

@Entity
@DiscriminatorValue("C")
@Table(name="ENCAISSEMENT_CHEQUE")
public class Encaissement_Cheque extends Encaissement_Technique{

@Column(name="ETAT_AVISCREDIT")
    private String etat_avisCredit;
    @Embedded
    private Cheque cheque;

    public Encaissement_Cheque(String etat_avisCredit, Date date_encaissement){
        this.etat_avisCredit=etat_avisCredit;
        this.date_encaissement=date_encaissement;
    }

    public Encaissement_Cheque(){

    }

}

====================entity Encaissement_Virement=======================

@Entity
@DiscriminatorValue("V")
@Table(name="ENCAISSEMENT_VIREMENT")
public class Encaissement_Virement extends Encaissement_Technique{

@Temporal(TemporalType.DATE)
    @Column(name="DATE_VIREMENT")
    private Date date_virement;

    public Encaissement_Virement(Date date_virement, String libelle, String piece_jointe, Float primCoass, Date date_encaissement){
        this.date_virement=date_virement;
        this.primeCoass=primCoass;
        this.piece_jointe=piece_jointe;
        this.libelle=libelle;
        this.date_encaissement=date_encaissement;
    }

    public Encaissement_Virement(){

    }

}

I created from eclipse an instance of Encaissement_virement, the generated id is 71 then when i create another it gives it 75, after that it jumps to 91 !!! then it's adding 3 after each insertion (i mean id=103 then 106 then 109) !!!!!

PS : When i excute

Encaissement_virement ev=new Encaissement_virement();
//ev.getters and setters

ev.getId_Encaissement(); doesn't return the real Id instead it returns 0 whether it is called before or after the commit.

This problem is driving me crazy and i can't figure out the problem. (i think that the inheritance id or the id type (which is long) are not working).

I would appreciate your help. Thanks.

Edit :

After using sql developer i'm convinced that the problem is in the inheritance.

This is what i get when i try to insert an Encaissement_virement (which is logical because it extends another entity) :

Can anyone help me fix the inheritance problem in jpa eclipselikn. Thanks again.

Edit2

I create a new instance of Encaissement_virement using this code :

JPAContainer<Encaissement_Espece> ee=JPAContainerFactory.make(Encaissement_Espece.class, PERSISTANCE_UNIT);
Encaissement_Espece encaissEspece;

JPAContainer<Encaissement_Cheque> ec=JPAContainerFactory.make(Encaissement_Cheque.class, PERSISTANCE_UNIT);
Encaissement_Cheque encaissCheque;

JPAContainer<Encaissement_Virement> ev=JPAContainerFactory.make(Encaissement_Virement.class, PERSISTANCE_UNIT);
Encaissement_Virement encaissVirement;
//Some code here.

                switch (typeEncaissement) {
            case 0:
                encaissEspece=new Encaissement_Espece();
                encaissEspece.setCaisse(caisseEnc);
                encaissEspece.setDate_encaissement(dateEnc);
                encaissEspece.setLibelle(libelleEnc);
                encaissEspece.setMontant(montantEncEspece);
                encaissEspece.setPiece_jointe(pieceJointesEnc);
                encaissEspece.setClient(clientEnc);

                //UUID uuid = (UUID)ee.addEntity(encaissEspece);
                //EntityItem<Encaissement_Espece> eeEntityItem = ee.getItem(uuid);

                IdEnc=(Long)ee.addEntity(encaissEspece);
                ee.commit();
                break;
            case 1:
                encaissCheque=new Encaissement_Cheque();
                encaissCheque.setBanque(banqueEnc);
                encaissCheque.setCheque(chequeEnc);
                encaissCheque.setClient(clientEnc);
                encaissCheque.setDate_encaissement(dateEnc);
                encaissCheque.setLibelle(libelleEnc);
                encaissCheque.setPiece_jointe(pieceJointesEnc);
                encaissCheque.setPrimeCoass(primeCoassEnc);

                IdEnc=(Long)ec.addEntity(encaissCheque);
                ec.commit();
                break;
            case 2:
                encaissVirement=new Encaissement_Virement();
                encaissVirement.setBanque(banqueEnc);
                encaissVirement.setClient(clientEnc);
                encaissVirement.setDate_encaissement(dateEnc);
                encaissVirement.setLibelle(libelleEnc);
                encaissVirement.setPiece_jointe(pieceJointesEnc);
                encaissVirement.setPrimeCoass(primeCoassEnc);
                encaissVirement.setDate_virement(dateVir);

                IdEnc=(Long)ev.addEntity(encaissVirement);
                ev.commit();
                break;

            default:
                break;

this is the result (a new anstance with 66 as id) : [EL Fine]: sql: 2015-04-01 17:25:45.301--ServerSession(5981709)--Connection(27335918)--Thread(Thread[http-bio-8080-exec-5,5,main])--SELECT ENCAISSEMENT_SEQ.NEXTVAL FROM DUAL

[EL Fine]: sql: 2015-04-01 17:25:45.316--ClientSession(26722029)--Connection(27335918)--Thread(Thread[http-bio-8080-exec-5,5,main])--INSERT INTO ENCAISSEMENT (ID_ENCAISSEMENT, DATE_ENCAISSEMENT, LIBELLE, PIECE_JOINTE, AGENCE, DATE_AVIS_RECETTE, ENDROIT, PROVENANCE, UNITE, ID_CLIENT, ENCAISS_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [66, 2015-04-01, titre, jointes, null, null, null, null, null, 2, V]

[EL Fine]: sql: 2015-04-01 17:25:45.795--ClientSession(26722029)--Connection(27335918)--Thread(Thread[http-bio-8080-exec-5,5,main])--INSERT INTO ENCAISSEMENT_TECHNIQUE (PRIMECOASS, ID_BANQUE, ID_ENCAISSEMENT) VALUES (?, ?, ?) bind => [123.0, 1, 66]

[EL Fine]: sql: 2015-04-01 17:25:45.936--ClientSession(26722029)--Connection(27335918)--Thread(Thread[http-bio-8080-exec-5,5,main])--INSERT INTO ENCAISSEMENT_VIREMENT (DATE_VIREMENT, ID_ENCAISSEMENT) VALUES (?, ?) bind => [2015-04-01, 66]

Then i drop this row and create another instance with the same code, and i get :

[EL Fine]: sql: 2015-04-01 17:30:42.119--ServerSession(27444543)--Connection(6011948)--Thread(Thread[http-bio-8080-exec-15,5,main])--SELECT ENCAISSEMENT_SEQ.NEXTVAL FROM DUAL

[EL Fine]: sql: 2015-04-01 17:30:42.13--ClientSession(1011455)--Connection(6011948)--Thread(Thread[http-bio-8080-exec-15,5,main])--INSERT INTO ENCAISSEMENT (ID_ENCAISSEMENT, DATE_ENCAISSEMENT, LIBELLE, PIECE_JOINTE, AGENCE, DATE_AVIS_RECETTE, ENDROIT, PROVENANCE, UNITE, ID_CLIENT, ENCAISS_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [69, 2015-04-01, titre, j, null, null, null, null, null, 4, V]

[EL Fine]: sql: 2015-04-01 17:30:42.195--ClientSession(1011455)--Connection(6011948)--Thread(Thread[http-bio-8080-exec-15,5,main])--INSERT INTO ENCAISSEMENT_TECHNIQUE (PRIMECOASS, ID_BANQUE, ID_ENCAISSEMENT) VALUES (?, ?, ?) bind => [123.0, 1, 69]

[EL Fine]: sql: 2015-04-01 17:30:42.211--ClientSession(1011455)--Connection(6011948)--Thread(Thread[http-bio-8080-exec-15,5,main])--INSERT INTO ENCAISSEMENT_VIREMENT (DATE_VIREMENT, ID_ENCAISSEMENT) VALUES (?, ?) bind => [2015-04-01, 69]

You can notice that instead of getting 67 as Id i get 69 ??

来源:https://stackoverflow.com/questions/29372233/id-generator-in-jpa-eclipselink-inheritance-return-random-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!