Skip to main content

@Lob annotation causing an error?

8 replies [Last post]
sprite64
Offline
Joined: 2007-11-12
Points: 0

I've been testing out the JPA for a coule of weeks or so. I'm using Netbeans 6 (Java SE - creating desktop apps) and SQL server. I've got CRUD stuff working on some basic tables I created, no probs - and generally I really like the idea of using this in future apps. I then decided to hook up to the Northwind database and try it out on a couple of the tables.

I picked the 'categories' table and got the entity class created etc. On trying to insert a new record I got the following:
javax.persistence.RollbackException: java.lang.NullPointerException

I spent hours going through this trying to find my error. I created a new table and gradually built up the same structure as the Categories table - adding a column at a time. Eventually, I discovered that if I removed the @Lob annotation from the Description column (ntext) and the Picture column (image) then the insert works fine. If I put it back I get the error.

Of course, I MAY be doing something else wrong - but I think it's something to do with the JPA. We turned on the profiler on SQL Server and the insert statement wasn't even hitting the database.

I'm wondering what the consequences are of leaving the @Lob annotation off for datatypes like ntext, image etc? Has anyone else had this problem?

The code I'm using to test an insert into the Categories table is as follows.
(The entitymanager is a protected field within the same class)

public void testCategories()
{
CategoryService cs = new CategoryService(em);
try{
this.em.getTransaction().begin();
Categories cat = cs.createCategory("Sewing");
this.em.getTransaction().commit();
System.out.println(cat.getCategoryID());
}
catch(javax.persistence.RollbackException ex)
{
System.out.println(ex.toString());
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sprite64
Offline
Joined: 2007-11-12
Points: 0

Talking to myself again here! Maybe it will help someone else. I just downloaded the jTDS driver (http://jtds.sourceforge.net/) and it seems to work. I removed my persistence unit, and added a new one - went through the steps of adding a new DB connection, new driver etc. and then adding my entity class to the PU. Ran my same bit of code WITH the @Lob annotation present and it worked fine. I then wondered if I hadn't tried the SQL 2005 driver properly - so I went through exactly the same steps and it broke again.

I now have a bit of a dilema as to whether I should be using the jTDS driver. Time for more testing I guess, and a chat with my database chap!

I *think* this answers my question - but any further info on this would be useful.

sprite64
Offline
Joined: 2007-11-12
Points: 0

Decided to try a different driver, as suggested. The SQL 2005 driver seemed to be backward compatible with 2000 so we downloaded that to give it a go. No good. I missed the fact that microsoft decided to change the class name from com.microsoft.jdbc.sqlserver.SQLServerDriver to com.microsoft.sqlserver.jdbc.SQLServerDriver (info here:http://blogs.msdn.com/jdbcteam/archive/2007/06/15/java-lang-classnotfoundexception-com-microsoft-jdbc-sqlserver-sqlserverdriver.aspx)

Made sure I was using the latest version of the 2000 drivers - same error. Surely I can't be the only person with this problem? Off to check if I have definately got the latest version of the JPA stuff.

lancea
Offline
Joined: 2003-06-13
Points: 0

can u post a simple app which shows the problem?

sprite64
Offline
Joined: 2007-11-12
Points: 0

Ok, I'll try!

I'm using SQL Server 2000 and Netbeans 6. Have attached a script to create the very simple table I used. Have also attached the files from the Java project (couldn't attach the whole zipped up nb project as it said the file was too large) You will need to edit the persistence.xml to add the username, password, database info etc.

Would be interested in any ideas as to what the problem is - it may well be that I'm missing something - I'm very new to the JPA.

chris_delahunt
Offline
Joined: 2005-07-06
Points: 0

Hi,

Can you also post the NPE stacktrace as well? This might help track down where in the code the problem lies.

Regards,
Chris

sprite64
Offline
Joined: 2007-11-12
Points: 0

[TopLink Info]: 2007.11.13 03:34:02.828--ServerSession(20196260)--TopLink, version: Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))
[TopLink Info]: 2007.11.13 03:34:03.250--ServerSession(20196260)--file:/C:/Documents%20and%20Settings/sharonc/My%20Documents/NetBeansProjects/Modules/lobtest/build/classes/-lobtestPU login successful
[TopLink Warning]: 2007.11.13 03:34:03.328--UnitOfWork(19419092)--java.lang.NullPointerException
[TopLink Warning]: 2007.11.13 03:34:03.328--UnitOfWork(19419092)--java.lang.NullPointerException
javax.persistence.RollbackException: java.lang.NullPointerException
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:120)
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:60)
at lobtest.Tests.testTableE_V2(Tests.java:38)
at lobtest.TestHarness.main(TestHarness.java:22)
Caused by: java.lang.NullPointerException
at com.microsoft.jdbc.sqlserver.tds.TDSRPCParameter.initializeUserParam(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.addUserParametersToRPC(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:647)
at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:703)
at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:492)
at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:452)
at oracle.toplink.essentials.internal.sessions.AbstractSession.executeCall(AbstractSession.java:690)
at oracle.toplink.essentials.internal.queryframework.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
at oracle.toplink.essentials.internal.queryframework.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:214)
at oracle.toplink.essentials.internal.queryframework.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:346)
at oracle.toplink.essentials.internal.queryframework.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:191)
at oracle.toplink.essentials.internal.queryframework.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:205)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:564)
at oracle.toplink.essentials.queryframework.InsertObjectQuery.executeCommit(InsertObjectQuery.java:89)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.performUserDefinedWrite(DatabaseQueryMechanism.java:750)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.performUserDefinedInsert(DatabaseQueryMechanism.java:714)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.insertObjectForWriteWithChangeSet(DatabaseQueryMechanism.java:602)
at oracle.toplink.essentials.queryframework.WriteObjectQuery.executeCommitWithChangeSet(WriteObjectQuery.java:162)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:390)
at oracle.toplink.essentials.queryframework.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:109)
at oracle.toplink.essentials.queryframework.DatabaseQuery.execute(DatabaseQuery.java:628)
at oracle.toplink.essentials.queryframework.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:555)
at oracle.toplink.essentials.queryframework.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:138)
at oracle.toplink.essentials.queryframework.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:110)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2233)
at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:952)
at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:909)
at oracle.toplink.essentials.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:269)
at oracle.toplink.essentials.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:190)
at oracle.toplink.essentials.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:2657)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1044)
at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:403)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1126)
at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:107)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:856)
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:102)
... 3 more

chris_delahunt
Offline
Joined: 2005-07-06
Points: 0

Hello,

I don't think this is a problem with the Lob annotation itself, but a driver problem. Searching for the error, I found
http://www.mydatabasesupport.com/forums/ms-sqlserver/223376-bug-setnull-...
and
http://lists.ofbiz.org/pipermail/users/2004-October/005348.html

Which suggest the problem is with the setNull() method on blobs and clobs. If possible, can you try a different driver or driver version and see if the problem is resolved. If not, the first post suggested using setBinaryStream and passing in null as a workaround - a bug should be entered to see if TopLInk Essentials can work around the issue on this platform.

Best Regards,
Chris

sprite64
Offline
Joined: 2007-11-12
Points: 0

That's interesting, thanks. I'll check it out tomorrow when I'm back at work. It's really bugging me as I really want to use the JPA - it's a great thing! The first project I've got in mind for it should be ok as I don't have any lobs in the database, but I'd like to get to the bottom of the problem if poss.