2016-06-02 10 views
0

Ich versuche, eine übergeordnete Entität zu bestehen, die mit einer anderen Kind Einheit verbunden ist, aber das Problem ist, dass die ID nicht für dieses Kind wird erzeugt, wenn persistierende so dass ich diesen Fehler habe: [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ORA-01400: cannot insert NULL into ("L2S$OWNER"."SABRI"."TRANSITION_MATRIX_ID")Persisting 2 Tabelle mit demselben erzeugt id

gibt es das Kind Entity:

@Data 
@Entity 
@IdClass(MyLibrarySabriEntityPK.class) 
@Table(name = "SABRI", schema = "L2S$OWNER", catalog = "") 

public class MyLibrarySabriEntity extends ActionForm { 
@Access(AccessType.FIELD) 
@Id 
@ManyToOne 
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX") 
private MyLibraryTestEntity sabriEntity; 

@Id 
private String RATING_ID_ROW; 

@Id 
private String RATING_ID_COL; 


@Basic 
@Column(name = "TRANSITION_PROBABILITY", nullable = true, insertable = true, updatable = true, precision = 20) 
private Double TRANSITION_PROBABILITY;} 

die PK-Klasse:

@Data 
public class MyLibrarySabriEntityPK implements Serializable { 
private String TRANSITION_MATRIX_ID; 
private String RATING_ID_ROW; 
private String RATING_ID_COL; 


public MyLibrarySabriEntityPK(String TRANSITION_MATRIX_ID,String RATING_ID_COL,String RATING_ID_ROW){ 
    this.TRANSITION_MATRIX_ID=TRANSITION_MATRIX_ID; 
    this.RATING_ID_COL = RATING_ID_COL; 
    this.RATING_ID_ROW= RATING_ID_ROW; 
} 

}

ist das Mutterunternehmen:

@Data 
@Entity 
@Table(name = "TEST", schema = "L2S$OWNER", catalog = "") 
public class MyLibraryTestEntity extends ActionForm { 

    @Access(AccessType.FIELD) 
    @OneToMany(mappedBy = "sabriEntity", cascade = CascadeType.PERSIST) 
    private final List<MyLibrarySabriEntity> entities = new ArrayList<MyLibrarySabriEntity>(25); 

    public void addEntitysabri(MyLibrarySabriEntity entity) { 
     getEntities().add(entity); 
     entity.setSabriEntity(this); 
    } 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "IdGenerated") 
    @GenericGenerator(name = "IdGenerated", strategy = "dao.Identifier") 
    @Column(name = "ID_TRANSITION_MATRIX", nullable = false, insertable = false, updatable = false, length = 10) 
    private String ID_TRANSITION_MATRIX; 


    @Basic 
    @Column(name = "REFERENCE", nullable = true, insertable = true, updatable = true, precision = 0) 
    private Integer reference;} 

Und hier versuche ich, die übergeordnete Tabelle zu bestehen, die auch die untergeordnete Tabelle bestehen bleiben soll, aber die Id nicht erzeugt wird!

MyLibrarySabriEntity Entity = null; 
MyLibraryTestEntity test = getMyLibraryTestEntity(matrixStartDate, matrixName); // here I get the values of my entity test (parent) 
    try { 
     transaction.begin(); 
     for (int row = 0; row < 20; row++) { 
      for (int col = 0; col < 20; col++) { 
       double val = cells.get(row + FIRST_ROW, col + FIRST_COL).getDoubleValue(); 
       Entity = getMyLibrarySabriEntity(col, row, val); // this get the values of the Entity parameters (child) 
       Entity.setSabriEntity(test); 
       test.addEntitysabri(Entity); 
       em.persist(test); 
      } 
     } 


    } catch (Exception e) { 
     if (transaction.isActive()) 
      transaction.rollback(); 
     LOGGER.warn(e.getMessage(), e); 

    } finally { 
     if (transaction.isActive()) 
      transaction.commit(); 
     em.close(); 

    } 
+0

sieht so aus, abhängige Beans nicht richtig zu konfigurieren, bitte erneut prüfen. –

+0

Warum sagt der Titel "same generated id"? Die untergeordnete Entität 'MyLibraryTestEntity' hat eine eigene ID und eine übergeordnete Entität hat ihre eigene, vorausgesetzt, Sie konfigurieren die Beans-Abhängigkeit richtig. – Prashant

+0

MyLibraryTestEntity es ist die übergeordnete Entität und die untergeordnete Entität ist MyLibrarySabriEntity, Sie haben recht, es ist nicht die gleiche ID, weil die Kind-ID 3 ID hat, also ich meine, der Wert der generierten ID der übergeordneten Entität ist gleich dem Wert der Spalte TRANSITION_MATRIX_ID meiner untergeordneten Entität – sabri

Antwort

0

Sie Unter der Annahme, verwenden JPA 2.0+

diese Zuordnung vollständig entfernen:

@Id 
@Column(name = "TRANSITION_MATRIX_ID", nullable = false, 
     insertable = true, updatable = true, length = 100) 
private String TRANSITION_MATRIX_ID; 

und legte die @Id direkt am ManyToOne und die einführbare und aktualisierbar Attribute entfernen.

@Access(AccessType.FIELD) 
@Id 
@ManyToOne 
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX") 
private MyLibraryTestEntity sabriEntity; 

Aktualisieren Sie Ihre ID-Klasse entsprechend. Jede frühere Bezugnahme auf TRANSITION_MATRIX_ID sollte durch eine Bezugnahme auf sabriEntity ersetzt werden. Sie verwirren auch @EmbeddedId und @IdClass: Nur Erstere würde Spaltendefinitionen enthalten, während Sie den letzteren Ansatz verwenden.

public class MyLibrarySabriEntityPK implements Serializable { 

    private String sabriEntity; 
    private String RATING_ID_ROW; 
    private String RATING_ID_COL; 
} 

See:

https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0

+0

Ich habe das getan, aber ich habe diesen Fehler: "Kann keine Eigenschaften (TRANSITION_MATRIX_ID) in einer mit @IdClass: model.MyLibrarySabriEntity annotierten Entität finden" und das Problem ist, dass ich sie nicht entfernen kann, weil ich 3 habe Spalte als Id nicht nur 2! – sabri

+0

Die Eigenschaft in der ID-Klasse sollte mit der Eigenschaft in der Entität übereinstimmen, so dass "sabriEntity" und nicht "TRANSITION_MATRIX_ID" –

+0

Ich habe versucht, es zu entfernen, aber ich hatte den gleichen Fehler "kann NULL nicht einfügen (" L2S $ OWNER "). "SABRI". "TRANSITION_MATRIX_ID") ". – sabri

0

Danke an Alan Hay, fand ich das Problem, ich habe die Eigenschaft TRANSITION_MATRIX_ID meiner IdClass zu sabriEntity ändern, und ich all die Annotation dieser Klasse löschen!

Kinder Einheit

@Data 
@Entity 
@IdClass(MyLibrarySabriEntityPK.class) 
@Table(name = "SABRI", schema = "L2S$OWNER", catalog = "") 
public class MyLibrarySabriEntity extends ActionForm { 

@Access(AccessType.FIELD) 
@ManyToOne 
@Id 
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX") 
private MyLibraryTestEntity sabriEntity; 


@Id 
private String RATING_ID_ROW; 

@Id 
private String RATING_ID_COL; 


@Basic 
@Column(name = "TRANSITION_PROBABILITY", nullable = true, insertable = true, updatable = true, precision = 20) 
private Double TRANSITION_PROBABILITY; 

geordnete Entität

@Data 
@Entity 
@Table(name = "TEST", schema = "L2S$OWNER", catalog = "") 
public class MyLibraryTestEntity extends ActionForm { 

@Access(AccessType.FIELD) 
@OneToMany(mappedBy = "sabriEntity", cascade = CascadeType.PERSIST) 
private final List<MyLibrarySabriEntity> entities = new ArrayList<MyLibrarySabriEntity>(25); 

public void addEntitysabri(MyLibrarySabriEntity entity) { 
    getEntities().add(entity); 
    entity.setSabriEntity(this); 
} 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO, generator = "IdGenerated") 
@GenericGenerator(name = "IdGenerated", strategy = "dao.Identifier") 
@Column(name = "ID_TRANSITION_MATRIX", nullable = false, insertable = false, updatable = false, length = 10) 
private String ID_TRANSITION_MATRIX; 


@Basic 
@Column(name = "REFERENCE", nullable = true, insertable = true, updatable = true, precision = 0) 
private Integer reference; 

PK Klasse

@Data 
public class MyLibrarySabriEntityPK implements Serializable { 
private MyLibraryTestEntity sabriEntity; 
private String RATING_ID_ROW; 
private String RATING_ID_COL; 

public MyLibrarySabriEntityPK() { 
} 

public MyLibrarySabriEntityPK(MyLibraryTestEntity sabriEntity,String RATING_ID_COL,String RATING_ID_ROW){ 
this.sabriEntity=sabriEntity; 
this.RATING_ID_COL = RATING_ID_COL; 
this.RATING_ID_ROW= RATING_ID_ROW; 

}

}