添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
潇洒的吐司  ·  Start AR Session ...·  8 月前    · 
俊秀的松球  ·  ファイルから AWS CLI ...·  1 年前    · 
聪明的啤酒  ·  The Apache Groovy ...·  1 年前    · 
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.
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;
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! :)
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!