-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate does not issue Insert, Foreign Key Exception
PostPosted: Tue Mar 03, 2009 1:49 pm 
Newbie

Joined: Tue Mar 03, 2009 1:41 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 4:57 pm 
Newbie

Joined: Tue Mar 03, 2009 1:41 pm
Posts: 2
A small update on things:

I tried running this after all the inserts and before the initialisation (and saving) of the child object:
Code:
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();


I got an error saying the transaction hadn't started yet (and therefore couldn't be committed).

So I figured all the queries were queued (?, more like a guess)
So I did:
Code:
hibernateTemplate.getSessionFactory().getCurrentSession().beginTransaction();
hibernateTemplate.flush();
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();


And was back to square one of the foreign key exception and no data in my database.

Anybody got a clue?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.