More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.
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?
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;
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!