-->
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.  [ 3 posts ] 
Author Message
 Post subject: can not create an object with JPA (id doesn't have a default
PostPosted: Thu Sep 02, 2010 11:27 am 
Beginner
Beginner

Joined: Tue Feb 12, 2008 3:53 pm
Posts: 38
Hi,

I work on an application using JPA and Mysql. I can't create an object "subject"

My object subject
Code:
public class Subject implements java.io.Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -6667237497292561396L;
   private Integer subId;
   private Integer subCenter;
        .... other properties.........

   @Id
   @Column(name = "SUB_ID", unique = true, nullable = false, precision = 9, scale = 0)
        @GeneratedValue(strategy = GenerationType.AUTO)
   public Integer getSubId() {
      return this.subId;
   }
....................
...................


The code where I create a subject

Code:
        Subject subject = new RSubject();
        subject.setSubCenter(centerId);
        .... setting other properties.........
        newSubject = (Subject)subjectDao.create(subject);


and I get the following error :

Code:
JDBCExceptionReporter [ERROR] Field 'SUB_ID' doesn't have a default value


I tried to set a default value to subId (private Integer subId =new Integer(0);), but I have the same error message

Thank you in advance for your answers


Top
 Profile  
 
 Post subject: Re: can not create an object with JPA (id doesn't have a default
PostPosted: Thu Sep 02, 2010 3:53 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The error message is from MySQL. Hibernate will not include a value for SUB_ID in the insert SQL since you have declared it as GenerationType.AUTO (which means that the database is supposed to generate the value). I think the problem is that SUB_ID is not an AUTO_INCREMENT field. See http://dev.mysql.com/doc/refman/5.1/en/ ... ement.html for an example.


Top
 Profile  
 
 Post subject: Re: can not create an object with JPA (id doesn't have a default
PostPosted: Sun Sep 12, 2010 5:46 am 
Beginner
Beginner

Joined: Tue Feb 12, 2008 3:53 pm
Posts: 38
Hi nordborg and thank you for your answer
effectively I had to add AUTO_INCREMENT for the column SUB_ID and it works
By the way, SEQUENCES don't exist for MYSQL ?


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