re: user_id has value, but get constraint exception of it being null, using JPA and Lift 1.1

1,444 views
Skip to first unread message

James Black

unread,
Dec 19, 2009, 2:24:40 AM12/19/09
to Lift
Hello,
I have struggled with this for a few hours and hopefully someone can
see what I am doing wrong.

Basically, I insert into users, I then select the user, I then set
that in my address and try to insert and get an error that the user_id
cannot be null, but it has a value.

I am including the two entities involved at the bottom, and the part
of the test that is executed. The last line in the test is where the
error is thrown.


Hibernate: insert into users (firstName, lastName, username, id)
values (?, ?, ?, ?)
Hibernate: select user0_.id as id105_, user0_.firstName as
firstName105_, user0_.lastName as lastName105_, user0_.username as
username105_ from users user0_ where user0_.username=?
Found User /* loaded from the database */
id: 11403264
firstName: Test lastName: User
username: testuser_resume

Address being persisted /*, user.id has a value */:
id: 0
addressLineOne: Some street address
user:
id: 11403264
firstName: Test lastName: User
username: testuser_resume
Hibernate: insert into address (addressLineOne, city, state, user_fk,
zipCode, id) values (?, ?, ?, ?, ?, ?)
13800 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL
Error: 20000, SQLState: 23502
13800 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column
'USER_ID' cannot accept a NULL value.
13800 [main] ERROR
org.hibernate.event.def.AbstractFlushingEventListener - Could not
synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not
insert: [jblack.resumeapp.lift.model.Address]
at org.hibernate.exception.SQLStateConverter.convert
(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert
(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2295)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2688)
at org.hibernate.action.EntityInsertAction.execute
(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
167)
at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush
(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit
(JDBCTransaction.java:137)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
at jblack.resumeapp.lift.model.TestJPAWeb.save_resume
(TestJPAWeb.scala:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run
(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively
(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate
(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate
(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate
(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild
(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild
(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate
(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate
(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute
(JUnit4TestSet.java:62)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet
(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute
(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess
(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main
(SurefireBooter.java:1009)
Caused by: java.sql.SQLIntegrityConstraintViolationException: Column
'USER_ID' cannot accept a NULL value.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException
(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown
Source)
at
org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException
(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException
(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown
Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown
Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown
Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement
(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate
(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch
(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2275)
... 40 more
Caused by: java.sql.SQLException: Column 'USER_ID' cannot accept a
NULL value.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException
(Unknown Source)
at
org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA
(Unknown Source)
... 51 more
Caused by: ERROR 23502: Column 'USER_ID' cannot accept a NULL value.
at org.apache.derby.iapi.error.StandardException.newException(Unknown
Source)
at org.apache.derby.impl.sql.execute.NormalizeResultSet.normalizeRow
(Unknown Source)
at org.apache.derby.impl.sql.execute.NormalizeResultSet.getNextRowCore
(Unknown Source)
at org.apache.derby.impl.sql.execute.DMLWriteResultSet.getNextRowCore
(Unknown Source)
at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown
Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown
Source)
... 45 more


@Entity
@Table{val name = "address"}
class Address {
@Id
@GeneratedValue(){val strategy = GenerationType.AUTO}
var id : Long = _

@Column{val nullable = false, val length=80}
var addressLineOne : String = ""
@Column{val nullable = false, val length=32}
var city : String = ""
@Column{val nullable = false, val length=2}
var state : String = ""
@Column{val nullable = false, val length=10}
var zipCode : String = ""

@OneToOne{val fetch = FetchType.EAGER, val cascade=Array
(CascadeType.PERSIST, CascadeType.REMOVE)}
@JoinColumn{val name = "user_fk", val nullable = false}
var user : User = _

override def toString = "id: " + id + "\naddressLineOne: " +
addressLineOne + "\nuser: " + user
}


@Entity
@Table{val name="users"}
class User {
@Id
@GeneratedValue(){val strategy = GenerationType.AUTO}
var id : Int = _

@Column{val nullable = false, val length=32}
var firstName : String = ""
var lastName : String = ""

@Column{val nullable = false, val unique = true, val length = 20}
var username : String = ""

override def toString = "id: " + id + "\nfirstName: " + firstName +
" lastName: " + lastName + "\nusername: " + username
}


@Test
def save_resume() = {
var em = emf.createEntityManager()
var tx = em.getTransaction()
tx.begin()
val user = new User
user.username = "testuser_resume"
user.firstName = "Test"
user.lastName = "User"
em.persist(user)
tx.commit()
val retrieved_user = em.createNamedQuery
("findUserByUsername").setParameter("username",
user.username ).getSingleResult.asInstanceOf[User]
println("Found User\n" + retrieved_user)

tx = em.getTransaction()
tx.begin()
val address = new Address
address.addressLineOne = "Some street address"
address.city = "Knoxville"
address.state = "TN"
address.zipCode = "379xx"
address.user = retrieved_user
println("Address being persisted:\n" + address)
em.persist(address)
tx.commit()

Derek Chen-Becker

unread,
Dec 28, 2009, 9:57:47 AM12/28/09
to lif...@googlegroups.com
OK, I see something really odd here. The exception is complaining about a missing "USER_ID" column, but there's no such column in either insert statement:


Hibernate: insert into users (firstName, lastName, username, id) values (?, ?, ?, ?)
Hibernate: insert into address (addressLineOne, city, state, user_fk, zipCode, id) values (?, ?, ?, ?, ?, ?)

Out of curiosity, do you happen to have a "user_id" column in the address table in addition to user_fk? How did you generate the tables for this? If Hibernate is doing the generation, one thing to note is that it won't ever remove columns, so if you previously had the "user_fk" column named "user_id", then you have both columns there now.

Derek


--

You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



Reply all
Reply to author
Forward
0 new messages