-->
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.  [ 4 posts ] 
Author Message
 Post subject: null value in column "id_user" violates not-null c
PostPosted: Sun Mar 22, 2009 1:39 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hello everyone,

I have a problem while trying to insert an user to my db.

Here is my User.java:
Code:
...
private int id = 0;
private String login = null;
private String password = null;
private Date creationDate = new Date();
private int idActivationState = 0;
private Profile profile = null;
private UserCategory userCategory = null;
private List<UserResource> resourceList = null;
private List<Article> articleList = null;
private List<Comment> commentList = null;
private Rating rating = null;
...


My user.hbm:
Code:
<hibernate-mapping package="com.elnominal.model.user" auto-import="false">

<class name="User" table="elnominal_user" lazy="true">

   <id name="id" type="integer" column="id">
      <generator class="sequence">
         <param name="sequence">elnominal_user_id_seq</param>
      </generator>
   </id>

   <property name="login" type="string" column="login" length="50" not-null="true" unique="true"/>
   <property name="password" type="string" column="password" length="50" not-null="true"/>
   <property name="creationDate" column="creation_date" type="java.util.Date" update="false" not-null="true"/>   
   <property name="idActivationState" type="integer" column="id_activation_state" not-null="true"/>
   
   <many-to-one name="profile" cascade="all" class="com.elnominal.model.user.Profile" column="id_profile" not-null="true"/>
   <many-to-one name="rating" cascade="all" class="com.elnominal.model.rating.Rating" column="id_rating" not-null="true"/>
   <many-to-one name="userCategory" cascade="none" class="com.elnominal.model.category.UserCategory" column="id_category" not-null="true"/>

   <bag name="resourceList" cascade="all" inverse="true">
      <key column="id_user"/>
      <one-to-many class="com.elnominal.model.resource.UserResource"/>
    </bag>

   <bag name="articleList" cascade="all" inverse="true">
      <key column="id_user"/>
      <one-to-many class="com.elnominal.model.article.Article"/>
    </bag>

   <bag name="commentList" cascade="all" inverse="true">
      <key column="id_user"/>
      <one-to-many class="com.elnominal.model.comment.Comment"/>
    </bag>

</class>

</hibernate-mapping>


My Resource.java (UserResource extends from it):
Code:
...
private int id = 0;
private String title = null;
private String description = null;   
private String fileName;
private String filePath;
private Date creationDate = new Date();
private ResourceCategory resourceCategory = null;
...


And my Resource.hbm:
Code:
<hibernate-mapping package="com.elnominal.model.resource">

<class name="Resource" lazy="true">

   <id name="id" type="integer" column="id">
      <generator class="sequence"/>
   </id>

   <property name="title" type="string" column="title" length="100" not-null="true"/>
   <property name="description" type="string" column="description" length="1000" not-null="false"/>
   <property name="fileName" type="string" column="file_name" length="100" not-null="true"/>
   <property name="filePath" type="string" column="file_path" length="200" not-null="true"/>
   <property name="creationDate" column="creation_date" type="java.util.Date" update="false" not-null="true"/>   

   <many-to-one name="resourceCategory" cascade="none" class="com.elnominal.model.category.ResourceCategory" column="id_category" not-null="true"/>

   <union-subclass name="ArticleResource" table="elnominal_article_resource" />
   <union-subclass name="UserResource" table="elnominal_user_resource" />

</class>

</hibernate-mapping>


And finally the exception i am getting everytime i try to insert an user:
Code:
GRAVE: ERROR: null value in column "id_user" violates not-null constraint
22-mar-2009 18:10:18 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
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:253)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at com.elnominal.persistence.HibernateUtil.commitTransaction(HibernateUtil.java:165)
   at com.elnominal.dao.UserDAO.makePersistent(UserDAO.java:171)
   at com.elnominal.servlet.controller.InsertUserServlet.service(InsertUserServlet.java:48)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.elnominal.servlet.SearchDataFilter.doFilter(SearchDataFilter.java:44)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.elnominal.servlet.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:104)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into elnominal_user_resource (title, description, file_name, file_path, creation_date, id_category, id) values (asdas, NULL, portada.jpg, D:/trabajo/proyectos/elnominal/web/upload/portada.jpg, 2009-03-22 18:10:16.671000 +01:00:00, 1, 58) was aborted.  Call getNextException to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   ... 29 more
22-mar-2009 18:10:18 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet InsertUserServlet lanz� excepci�n
com.elnominal.exception.InfrastructureException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at com.elnominal.persistence.HibernateUtil.commitTransaction(HibernateUtil.java:170)
   at com.elnominal.dao.UserDAO.makePersistent(UserDAO.java:171)
   at com.elnominal.servlet.controller.InsertUserServlet.service(InsertUserServlet.java:48)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.elnominal.servlet.SearchDataFilter.doFilter(SearchDataFilter.java:44)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.elnominal.servlet.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:104)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Thread.java:619)
Caused by: 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:253)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at com.elnominal.persistence.HibernateUtil.commitTransaction(HibernateUtil.java:165)
   ... 21 more
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into elnominal_user_resource (title, description, file_name, file_path, creation_date, id_category, id) values (asdas, NULL, portada.jpg, D:/trabajo/proyectos/elnominal/web/upload/portada.jpg, 2009-03-22 18:10:16.671000 +01:00:00, 1, 58) was aborted.  Call getNextException to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   ... 29 more


Can someone help me? i am stuck at this...

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 2:58 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You are not showing the code that makes this happens, but from the mapping files and the generated SQL it seems that you have not mapped the association between User and UserResource correctly. Since you have inverse="true" on User.resourceList you must map the non-inverse direction UserResource.user and also make sure that you call UserResource.setUser() to make sure both ends as set properly. Everything is explained in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... bidir.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 4:38 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Thank you nordborg,

It is now working good. I have a question, if i dont map inverse="true", does it mean that i dont need to map the non-inverse part in UserResource.user? if so, what is considered best practice?

Thanks a lot again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 1:29 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Hi vector,

if you don't map inverse = true it means that user is the owning side of this association (the user defines which resources belong to him).

In that case you don't need a bidirectional association so you don't need UserResource.user.

The recommended practice is to use bidirectional associations, where the many-side is the owner of the association. So you can keep it like you got it now.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.