-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 1:19 pm 
Newbie

Joined: Thu Jul 02, 2009 1:13 pm
Posts: 4
I've got a class annotated corrected (pretty sure anyway :)) and when I create an instance and attempt to presist the object I'm getting this error:

Code:
SEVERE: Cannot insert the value NULL into column 'log_id', table 'PersistenceTest.dbo.LOGENTRY'; column does not allow nulls. INSERT fails.


Here's the class:
Code:
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "LOGENTRY")
public class LogEntry {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private long log_id;
...
}


Here's the code I'm using to create/persist:
Code:
EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("FoodLogPU3");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();

userTransaction.begin();
LogEntry le = new LogEntry();
le.setEntry_desc("test");
le.setEstimated_calories(50);
em.persist(le);
userTransaction.commit();
em.close();
entityManagerFactory.close();


Here's the table definition (SQL Server 2005):
Code:
USE [PersistenceTest]
GO
/****** Object:  Table [dbo].[LOGENTRY]    Script Date: 07/02/2009 13:18:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LOGENTRY](
   [log_id] [int] NOT NULL,
   [Description] [nchar](50) NULL,
   [calories] [numeric](18, 0) NULL,
CONSTRAINT [PK_LOGENTRY] PRIMARY KEY CLUSTERED
(
   [log_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


I was under the impression that with GenerationType.AUTO hibernate would take care of generating the key..?? Any idea what I'm doing wrong here?


Top
 Profile  
 
 Post subject: Re: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 1:59 pm 
Newbie

Joined: Mon May 11, 2009 11:16 am
Posts: 13
I'm not sure, but maybe you try to switch the type of the log_id attribute from long to Long. It is strange that the error message says "value null". The long type is a java primitive, I guess, and a primitive should not be null in any case. Give it a try!

The rest looks correct


Top
 Profile  
 
 Post subject: Re: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 2:05 pm 
Newbie

Joined: Thu Jul 02, 2009 1:13 pm
Posts: 4
Same error - its like its not generating the key.

I might try a different type of database, but it should work so I don't know.


Top
 Profile  
 
 Post subject: Re: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 2:17 pm 
Newbie

Joined: Mon May 11, 2009 11:16 am
Posts: 13
Oh i think you need to implement serializable, or not?Furthermore add the serialVersionUID to the class. Try something like this

Code:
@Entity
@Table(name = "itbos_category")
public class Category implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * the database id for the category
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


Top
 Profile  
 
 Post subject: Re: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 2:25 pm 
Newbie

Joined: Thu Jul 02, 2009 1:13 pm
Posts: 4
lol - well I appreciate the effort but I'm getting the same thing.

I'll spend some time on it during the extended weekend and see if I can figure something out - plus I'll try a MySQL database. I had a lot of trouble trying to get this to work, originally tried OpenJPA but for some reason that couldn't open the connection to the database even though Netbeans was able to connect and build my persistence unit. So I switched to Hibernate JPA and now I can get to the database but can't insert a row....I guess if it was easy everyone would do it! :)


Top
 Profile  
 
 Post subject: Re: Hibernate JPA not auto generating primary key
PostPosted: Thu Jul 02, 2009 2:43 pm 
Newbie

Joined: Thu Jul 02, 2009 1:13 pm
Posts: 4
I got it - even though I had the field set as the primary key, I did not identify it as an "identity" column and set the start and increment values in SQL Server. Once I did that, the code is now working correctly. It appears that Hibernate must use the ID provided by the database in this case.

interesting...at least its working!


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