We're using a combination of H2, JPA annotations, Spring and Hibernate to develop our webapp. We're using H2 in compatiability mode with MODE=Oracle.
We have an ItSystem class that has an one-to-many association with an ItSystemAka class as follows:
@Entity
@Table(name="ITSYSTEM")
public class ItSystem implements Serializable {
private static final long serialVersionUID = 1L;
private static final String SEQ_NAME = "SeqName";
private static final String SEQUENCE = "sequence";
@Id
@GeneratedValue(generator = SEQ_NAME)
@GenericGenerator(name = SEQ_NAME,strategy = SEQUENCE, parameters = { @Parameter(name = SEQUENCE, value = "IT_SYSTEM_SEQ") })
@Column(name="ITSYSTEM_EDW_ID")
private BigDecimal itSystemEdwId;
@Column(name="ITSYSTEM_VERSION_ID")
private BigDecimal itSystemVersionId;
@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
@JoinColumn(name="ITSYSTEM_EDW_ID")
private Set<ItsystemAka> itSystemAkas;
.....
}
@Entity
@Table(name="ITSYSTEM_AKA")
public class ItSystemAka implements Serializable {
private static final long serialVersionUID = 1L;
private static final String SEQ_NAME = "SeqName";
private static final String SEQUENCE = "sequence";
@Id
@GeneratedValue(generator = SEQ_NAME)
@GenericGenerator(name = SEQ_NAME,strategy = SEQUENCE, parameters = { @Parameter(name = SEQUENCE, value = "IT_SYSTEM_AKA_SEQ") })
@Column(name="AKA_EDW_ID")
private BigDecimal akaEdwId;
private String aka;
@Column(name="ITSYSTEM_EDW_ID")
private BigDecimal itSystemEdwId;
....
}
We're using the following connection properties:
myDataSource.driverClassName=org.h2.Driver
myDataSource.url=jdbc:h2:~/test;MODE=Oracle;DB_CLOSE_DELAY=-1
myDataSource.username=sa
myDataSource.password=
sessionFactory.hibernateProperties[hibernate.dialect]=org.hibernate.dialect.H2Dialect
sessionFactory.hibernateProperties[hibernate.hbm2ddl.auto]=create
sessionFactory.hibernateProperties[hibernate.show_sql]=false
sessionFactory.hibernateProperties[hibernate.connection.autocommit]=false
sessionFactory.hibernateProperties[hibernate.format_sql]=true
If we have an instance of ItSystem with a number of ItSystemAka instances and we try and update the ItSystem instance to the database, then it is losing the reference to all the associated ItSystemAkas. Looking at the database with the H2 Console, I can see that the foregin key (IT_SYSTEM_EDW_ID) for the corresponding rows in the ITSYSTEM_AKA table are getting set to null.
I've tried using the Oracle Dialect instead of the native H2 Dialect as advised on the H2 website, but it produces the same results. I've also tried using an actual Oracle database instead of H2, in which case everything seems to work fine.
Any i开发者_开发知识库dea as to what is going wrong?
Any help would be much appreciated.
Regards, Priyesh
Dialect does not matter in this case.
You have unidirectional relation between tables if you use this mapping. The easy way to reorganize your mapping like this
@Entity
@Table(name="ITSYSTEM")
public class ItSystem implements Serializable {
....
@Id
@GeneratedValue(generator = SEQ_NAME)
@GenericGenerator(name = SEQ_NAME,strategy = SEQUENCE, parameters = { @Parameter(name = SEQUENCE, value = "IT_SYSTEM_SEQ") })
@Column(name="ITSYSTEM_EDW_ID")
private BigDecimal itSystemEdwId;
@OneToMany(fetch = FetchType.EAGER, mappedBy="itSystemEdwId", cascade={CascadeType.ALL})
private Set<ItsystemAka> itSystemAkas;
.....
}
@Entity
@Table(name="ITSYSTEM_AKA")
public class ItSystemAka implements Serializable {
...
@Id
@GeneratedValue(generator = SEQ_NAME)
@GenericGenerator(name = SEQ_NAME,strategy = SEQUENCE, parameters = { @Parameter(name = SEQUENCE, value = "IT_SYSTEM_AKA_SEQ") })
@Column(name="AKA_EDW_ID")
private BigDecimal akaEdwId;
...
@Parent
private ItSystem itSystemEdwId;
....
}
mappedBy always create bidirectional relation
精彩评论