aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problems with session.save(Object) in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problems with session.save(Object) in Hibernate" Watch "Problems with session.save(Object) in Hibernate" New topic
Author

Problems with session.save(Object) in Hibernate

Fabio Batalha
Greenhorn

Joined: Aug 11, 2004
Posts: 6
Hy everybody!

I have this small routine that verifies if the table ARTICLES already have a PID (primary key field), if not, i try to insert a new PID but when i try to insert I have this error!

First, this is the code:

for (Iterator<String> i = addArticlesDistinct.iterator(); i.hasNext(); ){
String PID = i.next();
System.out.println("Query: from articles where PID='"+PID+"'");
select = sessao.createQuery("from articles where PID='"+PID+"'");
objetos = (ArrayList) select.list();
System.out.println("records found: "+objetos.size());
if (objetos.size() == 0){
articles addArticle = new articles();
addArticle.setPID(PID);
addArticle.setAuthors_xml("<author>FABIO BATALHA</author>");
//System.out.println(addArticle.getPID());
//inputObjects.add(addArticle);
System.out.println("Insert in articles PID='"+addArticle.getPID()+"'");
sessao.save(addArticle);
}
}


Now this is the error:

Hibernate: /* from articles where PID='S0102-35862003000200008' */ select articles0_.PID as PID1_, articles0_.url as url1_, articles0_.authors_xml as authors3_1_, articles0_.title as title1_, articles0_.serial as serial1_, articles0_.keywords_xml as keywords6_1_, articles0_.year as year1_, articles0_.number as number1_, articles0_.volume as volume1_, articles0_.suppl as suppl1_, articles0_.insertion_date as insertion11_1_, articles0_.process_date as process12_1_ from articles articles0_ where articles0_.PID='S0102-35862003000200008'
records found: 0
Insert in articles PID='S0102-35862003000200008'
Query: from articles where PID='S1413-86702003000300003'
Hibernate: /* insert org.bireme.hibernate.articles */ insert into articles (url, authors_xml, title, serial, keywords_xml, year, number, volume, suppl, insertion_date, process_date, PID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:954)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1099)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.bireme.scieloorgupdate.RelatedArticlesUpdate.main(RelatedArticlesUpdate.java:98)
Caused by: java.sql.BatchUpdateException: Column 'url' cannot be null
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more
Java Result: 1


My question is, why PID field are not been set by Hibernate when it constructs the insert string.

Hibernate: /* insert org.bireme.hibernate.articles */ insert into articles (url, authors_xml, title, serial, keywords_xml, year, number, volume, suppl, insertion_date, process_date, PID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

This is the mapping file for the articles table.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Document : articles.hbm.xml.xml
Created on : 30 de Maio de 2006, 11:11
Author : fabio.santos
Description:
Mapping the database structure for the class profiles
-->
<hibernate-mapping package="org.bireme.hibernate">
<class name="articles" table="articles">
<!-- Identificador da classe -->
<id name="PID" column="PID">
<generator class="assigned"/>
</id>
<!-- Propriedades da classe -->
<property name="url" column="url"/>
<property name="authors_xml" column="authors_xml"/>
<property name="title" column="title"/>
<property name="serial" column="serial"/>
<property name="keywords_xml" column="keywords_xml"/>
<property name="year" column="year"/>
<property name="number" column="number"/>
<property name="volume" column="volume"/>
<property name="suppl" column="suppl"/>
<property name="insertion_date" column="insertion_date"/>
<property name="process_date" column="process_date"/>
<!-- Relacionamento da classe -->
<!--one-to-many name="profile_article" class="profile_article" cascade="save-update"/-->
</class>
</hibernate-mapping>

thanks,
Fabio Batalha
Fabio Batalha
Greenhorn

Joined: Aug 11, 2004
Posts: 6
I saw that this error happens when I don�t set one value for at least one instance variable of the object article.

Now my doubt is, I need to set a value for al atributes of my transient object before save it to the database?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Fabio Batalha:
I saw that this error happens when I don�t set one value for at least one instance variable of the object article.

Now my doubt is, I need to set a value for al atributes of my transient object before save it to the database?


You do when your database constraints demand it:

Column 'url' cannot be null


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with session.save(Object) in Hibernate