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?