Hey everybody!
I'm trying to find a solution to the following problem:
I'm developping an application that writes and read to an Oracle DB, I use Hibernate for the ORM.
I'll start off by posting my hibernate and Spring configuration:
Code:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@serverip:1521:XE" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
... List of mapped classes here ...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="incomingMailserverDAOTarget" class="com.webmailr.persistence.server.IncomingMailserverDAO">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<bean id="incomingMailserverDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="incomingMailserverDAOTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="mailDirectoryDAOTarget" class="com.webmailr.persistence.server.MailDirectoryDAO">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<bean id="mailDirectoryDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="mailDirectoryDAOTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
[... And so on, I have a DAOTarget and a TransactionProxyFactoryBean for every DAO ...]
The DAO's look like the following:
Code:
public class IncomingMailserverDAO{
private HibernateTemplate hibernateTemplate;
@Transactional
public void save(IncomingMailserver c)
{
this.hibernateTemplate.save(c);
}
@SuppressWarnings("unchecked")
public List<IncomingMailserver> selectAll()
{
return this.hibernateTemplate.find("from IncomingMailserver");
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
}
And the classes itself:
Code:
public class MailDirectory {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_MAILDIRECTORY")
@SequenceGenerator( name="SEQ_MAILDIRECTORY", sequenceName="SEQ_MAILDIRECTORY" )
private int id;
@OneToMany(mappedBy="mailDirectory")
private List<Conversation> conversation;
@OneToMany
private List<MailDirectory> subfolders;
@Column
private String name;
@OneToOne(cascade=CascadeType.ALL)
private IncomingMailserver server;
(methods here, normal POJO getters and setters)
}
Code:
public abstract class IncomingMailserver implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_INCOMINGMAIL")
@SequenceGenerator( name="SEQ_INCOMINGMAIL", sequenceName="SEQ_INCOMINGMAIL" )
private int id;
@Column
private Boolean ssl;
@Column
private Boolean tls;
@Column
private String domain;
@Column
private int port;
@Column
private Boolean secureAuthentication;
@OneToOne
private ServerAccount serverAccount;
@OneToOne(cascade=CascadeType.ALL)
private MailDirectory mailDirectory;
(methods here, normal POJO getters and setters)
}
Now when I write a few test objects to my database. It works just fine (just: create object, write, end of test). I can see my data in my database, no exception is thrown.
When however I do the following:
- Create instances of MailDirectory, IncomingMailserver and so on
- Write to database
- Create a child object of the MailDirectory object
- save the child object
I get the following kind of exception on a One-To-One relation object:
Code:
java.sql.BatchUpdateException: ORA-02291: integrity constraint (DBNAME.FK693E6569A078891) violated - parent key not found
The newly saved object intends to get his parent key (through recursive programming). But the instance is not saved to the DB yet.
Also when looking through my database, I find no data whatsoever.
My first guess was that the data didn't commit right. So I added this to every DAO Save methode:
Code:
hibernateTemplate.flush()
This didn't work, also I tried adding this to the hibernate properties:
Code:
<prop key="hibernate.connection.autocommit">true</prop>
I hope this is enough information to help me find a solution!
Thanks in advance,
Bart