Showing posts with label JPA. Show all posts
Showing posts with label JPA. Show all posts

Thursday, 18 August 2011

JPA not instantiating mapped collections.

I struggled a while now to figure out why I am getting a NullPointerException
when trying to access a mapped collection:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cmTypedef")

private List cmParamdefs;

I instantiated a object, then em.merge() it but still I got a NPE.
I really thought that merge would populate all the fields.


Then I tried to make it Eager:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cmTypedef",

fetch = FetchType.EAGER)
private List cmParamdefs = new ArrayList();
Still no luck :(


So merge didn't do the trick, but trying everything I saw that if I do
em.flush();

em.refresh(ret);
after the merge, then like magic the collection is populated..


Finally I remember I had this issue before and the simple solution I have so far is
to manually instantiate the collection:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cmTypedef")

private List cmParamdefs = new ArrayList();


I'm not sure if this is the best practice, but it works and is simple..

Wednesday, 27 October 2010

@Entity(name != table_name !!!

Normally if you get "org.hibernate.hql.ast.QuerySyntaxException: is not mapped" then you used the table name in stead of the class name. In my case I'm ashamed to admit, 
I though setting @Entity(name is setting the table name,
where in fact it is setting the JPQL alias! 
I thought it was weird that you can set that and the @Table(name . 


@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!
@Entity(name != table_name !!!


Thanks Matt Raible for showing me the light:

If you specify a name value for your @Entity annotation (for example
@Entity(name="person")), this will be the alias for HQL queries. If you
don't specify this value, the name will match the short name of your class (
Person). If you want to change the table name that's generated, use the
@Table
with a "name" value.

Tuesday, 12 October 2010

inconsistent datatypes with JPA.. eish

I've been having some weird errors with JPA eg.
(pasted here so you can find it with google)
==
INFO - batching 1 statements: 1: insert into dds.DDS_EMAIL_STATUS (DOC_REGISTER_ID, EMAIL_ADDRESS_FROM,
EMAIL_ADDRESS_TO, LAST_UPDATE_TIME, MESSAGE_ID, SEND_DATETIME, STATUS_ID, SUBJECT, DDS_EMAIL_STATUS_ID)
values (1001, 'dds-test@example.com', 'test@example.com', to_date('10/12/2010 12:41:05', 'mm/dd/yyyy
hh24:mi:ss'), NULL, to_date('10/12/2010 12:41:05', 'mm/dd/yyyy hh24:mi:ss'), 0, 'testSend',
800)  {executed in 4 msec}
INFO - executeBatch()
ERROR - 2. PreparedStatement.executeBatch() batching 2 statements:
1:  insert into dds.DDS_EMAIL_STATUS_LOG (DATE_TIME, DDS_EMAIL_STATUS_ID, MESSAGE, DDS_EMAIL_STATUS_LOG_ID) values (to_date('10/12/2010 12:41:05', 'mm/dd/yyyy hh24:mi:ss'), '', 'html body:
html body', 750)
java.sql.BatchUpdateException: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY
    at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
    at net.sf.log4jdbc.StatementSpy.executeBatch(StatementSpy.java:523)
    at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    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.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
    at org.apache.openejb.persistence.JtaEntityManager.flush(JtaEntityManager.java:130)
==
java.sql.SQLException: incompatible data types in combination in statement [alter table DDS_EMAIL_STATUS_LOG add constraint FKB7F2A8E6BAAD6FBB foreign key (DDS_EMAIL_STATUS_ID) references DDS_EMAIL_STATUS]
 at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
 at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
 at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
 at net.sf.log4jdbc.StatementSpy.executeUpdate(StatementSpy.java:694)
 at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
 at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
 at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:383)
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:341)
 at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:262)
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:211)
 at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:343)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
 at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
...
Caused by: org.hsqldb.HsqlException: incompatible data types in combination
 at org.hsqldb.error.Error.error(Unknown Source)
 at org.hsqldb.error.Error.error(Unknown Source)
 at org.hsqldb.Table.checkColumnsMatch(Unknown Source)
 at org.hsqldb.TableWorks.checkCreateForeignKey(Unknown Source)
 at org.hsqldb.TableWorks.addForeignKey(Unknown Source)
 at org.hsqldb.ParserDDL.processAlterTableAddForeignKeyConstraint(Unknown Source)
 at org.hsqldb.ParserDDL.processAlterTable(Unknown Source)
 at org.hsqldb.ParserDDL.processAlter(Unknown Source)
 at org.hsqldb.StatementSchema.getResult(Unknown Source)
 at org.hsqldb.StatementSchema.execute(Unknown Source)
 at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
 at org.hsqldb.Session.executeDirectStatement(Unknown Source)
 at org.hsqldb.Session.execute(Unknown Source)
==
javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
    at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:259)
    at org.apache.openejb.persistence.JtaEntityManager.remove(JtaEntityManager.java:107)
==
1:  update DDS_EMAIL_STATUS_LOG set DATE_TIME='10/12/2010 13:11:54.206', DDS_EMAIL_STATUS_ID='', MESSAGE='text body:
text body' where DDS_EMAIL_STATUS_LOG_ID=1 {FAILED after 0 msec}
java.sql.BatchUpdateException: data exception: string data, right truncation
 at org.hsqldb.jdbc.JDBCPreparedStatement.executeBatch(Unknown Source)
 at net.sf.log4jdbc.StatementSpy.executeBatch(StatementSpy.java:523)
 at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
 at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
 at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
==
I tried different databases because it looked to me like the hsqldb is getting a bit confused with the column order. But in the end it turned out that I made a bit of a mistake:
I converted an embedded complex primary key to have a generated Id. In stead of annotating the referenced object with a @ManyToOne(optional = false)
it was still using @Column(name = "DDS_EMAIL_STATUS_ID"). After much debugging, googling and figuring out how to print the actual sql using http://code.google.com/p/log4jdbc-remix/ I figured out that I neglegted to change @Column to @ManyToOne.

Why is nothing ever easy ;-P

Tuesday, 17 March 2009

Put persistence.xml in the war

I've been trying for days to get jpa, jndi, glassfish, hibernate and oracle to play together.

The main problem turns out that you have to put the persistence.xml file in your war file (project/web-root/WEB-INF/src/META-INF) not in your jar file.
I'll probably investigate more to see if it is somehow possible to put it in the jar, but for now my problems are solved.

... after some further experimentation I was able to put it in the jar, but I'm using <mapping-file>xx_orm.xml</mapping-file> now.