I just set up a basic hibernate/spring project to test some stuff. I use a MySQL db by using WAMP.
These are all of the classes: http://codepaste.net/7pwmtx
This is my bean definition and pom file: http://codepaste.net/4iz7jb
POM is a little bit messy but this is the problem: I think that I set up my dialect properly because I get no errors when i add something to the db. I get errors when I use find in hql. I t seems like hibernate isnt retrieving data from the same place that it is putting it. Any idea why this doesnt work?
By the way, the console output was:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateQueryException: hobject is not mapped [from hobject]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: hobject is not mapped [from hobject]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
at hibernate.dao.SpringHibernateOperatorImplementation.getAllRows(SpringHibernateOperatorImplementation.java:24)
at hibernate.main.HibernateMain.main(HibernateMain.java:22)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: hobject is not mapped [from hobject]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 5 more
Add the entity class hibernate.dao.HObject to hibernate.cfg.xml files. It is neeeded even if you are using annotation based configurations.
If you want to skip adding these to cfg file you can use the approach mentioned here provided you are using spring.
Related
I'm getting an error which looks like:
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: DirectAddress is not mapped [select d from DirectAddress d where emailaddress=?]
where query looks like
Query query = session.createQuery("select d from DirectAddress d where emailaddress=?");
and I have set an entity for javax
#Entity
#Table(name = "user")
Then I have tired that query:
Query query = session.createQuery("select d from " + DirectAddress.class.getName() + " d where emailaddress=?");
without any luck.
Also project is not using hibernate.cfg.xml file.
Any thoughts?
To use an entity you've created, make sure it's marked as #Entity using the javax library. You'll also need to map it so hibernate knows it's supposed to link it to your database table. Please verify if your entity is mapped in your applicationContext.xml (for spring applications) or persistence.xml in common hibernate applications.
Mapping your entities
You can map your entities in many ways. The most common methods are:
Mapping it directly in hibernate.cfg.xml:
<mapping class="your.packages.to.the.entity.class" />
Setting it up in your spring applicationContext.xml`s hibernate configurations
For a working example, please refer to:
How configure hibernate.cfg.xml to applicationContext.xml as dataSource?
By Spring Injection
You mentioned not using hibernate.cfg.xml. Are you injecting hibernate`s settings via spring (in a non-web application)? If so, please refer to Can we configure Hibernate Without hibernate.cfg.xml
If none of the solutions apply, please provide more details about your System (web, desktop, setting up hibernate programatically, etc) and i`ll edit with this answer with the most appropriate solution.
Cheers!
DirectAddress is not mapped [select d from DirectAddress d where emailaddress=?]
means hibernate did not find mapping for DirectAddress class.
Check persistence.xml file for entity mapping or you can directly add this class while initializing/loading Configuration.
I have just created and joined 3 new tables. Before creating them everything was working fine so I do believe the root cause is coming from their creation. Now when runned in the browser I receive this error:
org.apache.jasper.JasperException: Exception [EclipseLink-7242]
(Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often
occurs when an entity with an uninstantiated LAZY relationship is
serialized and that lazy relationship is traversed after
serialization. To avoid this issue, instantiate the LAZY relationship
prior to serialization.
How does one instantiate the LAZY relationship prior to serialization anyway? I've been looking all over the web but I still haven't found real working solutions...
I also receive this other error message:
Exception [EclipseLink-4002] (Eclipse Persistence Services -
2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Unknown column 't1.rating' in 'field list' Error Code: 1054 Call:
SELECT t1.id, t1.description, t1.last_update, t1.name, t1.price,
t1.rating FROM category_has_product t0, product t1 WHERE
((t0.category_id = ?) AND (t1.id = t0.product_id)) bind => [1
parameter bound] Query: ReadAllQuery(name="productCollection"
referenceClass=Product sql="SELECT t1.id, t1.description,
t1.last_update, t1.name, t1.price, t1.rating FROM category_has_product
t0, product t1 WHERE ((t0.category_id = ?) AND (t1.id =
t0.product_id))")
root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 't1.rating' in 'field list'
What do all those errors mean? I don't understand because all the columns and the rows are perfectly well mapped... It says it canno't find certain columns... I have checked all my tables over and over and I really don't understand what could possibly be wrong... Everything seems fine... A little help would be very much appreciated! Thanks
Besides I use the following for the app:
Netbeans
Glassfish
Mysql
Most pages in jsp
Problem with instantiate lazy reference is well known, so I will provide a link rather than explain myself: http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Serialization.2C_and_Detaching
Wiki page has also 3 solutions to solve the problem. But I will offer you another solution, which may suits your case: JSP usage.
Check https://dzone.com/articles/open-session-view-design (or just google for 'open session in view'. Many pages refers to Hibernate but it applies to Eclipselink also). The solution is start and end transaction in servlet filter. The transaction will cover whole request processing. In your case JSP page generation will be within transaction and lazy references will just work. Benefit is that you don't have to modify your code.
I have no idea about your second problem with mapping
I am upgrading Hibernate 3 to Hibernate 4.1. When running the application, I got below exception:
java.lang.AbstractMethodError
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:124)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2701)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1541)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1473)
at org.hibernate.loader.Loader.getRow(Loader.java:1373)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)
at org.hibernate.loader.Loader.doQuery(Loader.java:850)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2042)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3710)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:439)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:420)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:992)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:919)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:610)
at org.hibernate.type.EntityType.resolve(EntityType.java:438)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:150)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:995)
at org.hibernate.loader.Loader.doQuery(Loader.java:874)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2447)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2292)
at org.hibernate.loader.Loader.list(Loader.java:2255)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at my.com.MyDAOImpl.findDocuments(MyDaoImpl.java:22)......
I understand Hibernate4 changed parameters in nullSafeGet() and nullSafeSet(). So I have to override these methods using new parameters and ignored SessionImplementor in customized type implemetation anyway.
findDocuments() basically query documents from Documents table, none of the fields of Document.hmb.xml has customized type. My puzzle is why CustomType.nullSafeGet() is called? In Document.hmb.xml, there is only one suspect but I am not sure at the moment as after I changed the type to "Blob" and got same error.
<property name="image" column="PICTURE" type="org.springframework.orm.hibernate3.support.BlobByteArrayType"/>
Can you please help?
My question is resolved by changing "org.springframework.orm.hibernate3.support.BlobByteArrayType to "materialized_blob.
<property name="image" column="PICTURE" type="materialized_blob"/>
It looks like Spring implemenation calls Hibernate 3 CustomType.java which conflicts with the Hibernate 4 CustomType.java. One lesson learned, when upgrading Hibernate 3 to 4, blob column needs to use "materialized_blob" as type, which is much simpler solution than Hibernate 3, Spring has to put a lobHandler inside sessionFactory.
Hi don't understand the problem with namedQuery
<query name="updtae.payment.paymentDate.by.txn_id">
<![CDATA[
update Payment p set p.paymentDate =:payDate WHERE p.txnId=:txnId]]>
</query>
getting exception
HIbernate org.hibernate.HibernateException: Errors in named queries
no columns same us entity Name
It works fine in local And don't on production server
getSession().getNamedQuery("updtae.payment.paymentDate.by.txn_id").setTimestamp("payDate",paymentDate ).setString("txnId", txnId).executeUpdate();
Here is call of named query.
I fix problem buy changing hibernate version
from Hibernate-Version: 3.0.5
to Hibernate-Version: 3.2.1.ga
I have a 25 .xsd schemas and want to generate DB tables and POJO classes from this schemas.
Hibernate can do it? Im already try generate DB tables with XMLSpy and POJO with JiBX, but when i generate mapping with Hibernate names of tables in DB and in mappings was different. I want to use single tool to do this.
UPDATE
I try generate POJO with hibernate. I create \hibernate.cfg.xml ,hibernate.reveng.xml and console config. And when i try run configuration get error
org.hibernate.MappingException: Could not configure overrides from file: D:\liferay-develop\workspace\JABX_test\src\hibernate.reveng.xml
Could not configure overrides from file: D:\liferay-develop\workspace\JABX_test\src\hibernate.reveng.xml
org.hibernate.MappingException: invalid override definition
invalid override definition
org.hibernate.MappingException: invalid override definition
invalid override definition
org.xml.sax.SAXParseException: The content of element type "hibernate-reverse-engineering" must match "(schema-selection*,type-mapping?,table-filter*,table*)".
The content of element type "hibernate-reverse-engineering" must match "(schema-selection*,type-mapping?,table-filter*,table*)".
When i use empty TableFilter in reverse enginering hibernate create POJO but only from deafult postgres tables.
what i doing wrong?
UPDATE 2
How i use this tutorial http://www.youtube.com/watch?v=tfZiKn5xhXk and get error
org.hibernate.exception.JDBCConnectionException: Getting database metadata
Getting database metadata
java.sql.SQLException: No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
java.sql.SQLException: No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
UPDATE 3
Okey. Im reinstal eclipse and get situation like in first UPDATE again. So i try generate POJO from DB and get only POJO from postgis tables: geography_columns and spatial_ref_sys. Hibernate cant work with PostGis?