I need to execute select ST_AsText(column_name) from table using hibernate createSQlQuery().
When i executed that query, it fires an exception.
But when i execute the same query using simple JDBC or in my PGAdmin browser, the query works.
Below is my query:
select st_astext(linkPoints) from linkRoute
Exception:
SEVERE: ERROR: relation "linkroute" does not exist
Mapping File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lnt.utility.pojo.linkRoute" table="link_route">
<id name="Id" column="id">
<generator class="assigned" />
</id>
<property name="linkName" column="link_name"/>
<property name="distance" column="distance"/>
<property name="idNo" column="idno"/>
<property name="speed" column="speed"/>
<property name="linkPoints" column="link_points"/>
</class>
</hibernate-mapping>
Pls help
Hibernate provide a createSQLQuery method to let you call your native SQL statement directly.
I hope you should be able to work with your query using nativequery else you should go for Hibernate Spatial.
Please find the link for more information on hibernate native query tutorial. Hope this helps.
Related
Im trying to insert and select data in mysql database trough Hibernate and Insert is working fine for me but select somehow dont map the right table name and returns me no result.
Get and insert code:
SessionFactory sessFact = HibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
try {
Session mysession = HibernateUtil.getSessionFactory().getCurrentSession();
mysession.beginTransaction();
weatherDataObject resultObjectHib = (weatherDataObject) mysession.get(weatherDataObject.class, 26);
mysession.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
sessFact.close();
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="some.pack">
<class name="weatherCoordinates" table="coordinates">
<id name="dataBaseId" column="coordinates_id">
<generator class="native" />
</id>
<property name="lat" type="string" column="coordinates_lat" />
<property name="lon" type="string" column="coordinates_lon" />
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="some.pack">
<class name="weatherDataObject" table="data_object">
<id name="id" column="data_object_id">
<generator class="native" />
</id>
<property name="name" type="string" column="data_object_name" />
<many-to-one name="coord" class="task.main.DataObjects.weatherCoordinates"
column="coordinates_id" unique="true" not-null="true" cascade="all" />
</class>
</hibernate-mapping>
When I see sql execution strings it is :
Hibernate: insert into coordinates (coordinates_lat, coordinates_lon) values (?, ?)
Hibernate: insert into data_object (data_object_name, coordinates_id) values (?, ?)
Hibernate: select weatherdat0_.data_object_id as data_obj1_1_0_, weatherdat0_.data_object_name as data_obj2_1_0_, weatherdat0_.coordinates_id as coordina3_1_0_ from data_object weatherdat0_ where weatherdat0_.data_object_id=?
The problem is weatherdat0 that somehow is wrong my table is called the way I mapped it in the file data_object don't know how and why it is changed anybody can help ?
The query is generated on correct table, as the select query is run on table from data_object
The weatherdat0_is just an alias for the table as mentioned in the from statement:
from data_object weatherdat0_
So it is picking correct table name only.
Now if the query is not returning any results means there are no records matching that id so I suggest you to run the query directly on database and see if it returns any records.
I'm using Eclipse Kepler.
In a hibernate mapping, exist two ways of setting a property tag:
Inline:
<property name="usrname" column="usr_name" type="string" not-null="true" length="64" />
And split:
<property name="usrname" type="string">
<column name="usr_name" not-null="true" length="64"/>
</property>
The Hibernate 3.0 XML Editor does not give error with validation using the second form of property tag, but gives error with the first form.
I'm using this DTD for the mapping:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Why is this happening if the hibernate documentation for object mapping shows an example of property tag inline?
Thanks.
The errors that appeared in my mappings where related to attribute not known: column, type, not-null, length while using inline tags.
Fortunately the errors were solved using the following DTD and cleaning my project compilation:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
I have a Hibernate 3 mapping of a table with a medium text field. The table has a few thousands records, other than the medium text field, other fields do not hold much data at all.
The whole table needs to be loaded into memory Except the medium text field, which is very rarely used.
Although I have heard of lazy fetching annotation, however this legacy piece of code is using Hibernate XDoclet, which does not mention lazy fetching in its hibernate property definition:
http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html##hibernate_property_0_1_
So is there any way to tell Hibernate only to retrieve the value when its getter is called?
Hibernate default supportd lazy fetching unless exlplicity set it to Eager fetching.
If you want to get some idea on lazy fetching refer this
If it is property laza loading try something like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="db.hibernateSimple.Copay"
table="EHR_COPAY"
polymorphism="explicit"
>
<id
name="id"
column="COPAY_ID"
type="long"
unsaved-value="null"
>
<generator class="sequence">
<param name="table">HIVAL</param>
<param name="column">NEXT</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Copay.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="balance"
type="int"
column="balance"
length="10"
/>
<property
name="date"
type="java.lang.String"
column="OWNERSHIP_DATE"
/>
<property
name="comments"
type="java.lang.String"
column="comments"
lazy="true"
/>
<many-to-one
name="user"
class="db.hibernateSimple.User"
cascade="none"
outer-join="auto"
column="USER_ID_SEQ"
/>
I extracted the above from here you can get some idea I guess
I tried using lazy=true on the Hibernate property and it does not work, Hibernate still fetches the entire table even when the property is not geted.
As a workaround, I removed the Hibernate property and replaced it by JDBC query in getter, and JDBC statement in setter.
I'm just learning how to use Hibernate for database modeling/mapping. I'm using the following code snippet to get a User model.
Session session = HibernateUtil.getSessionFactory().openSession();
User user = (User) session.get(User.class, id);
session.close();
The user's configuration file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="c3.data.User">
<id name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>
<property name="email" type="java.lang.String"/>
<property name="salt" type="java.lang.String"/>
<one-to-one name="authSession" property-ref="user" class="c3.data.AuthSession"/>
<set name="projects" inverse="true">
<key column="projectLeadId"/>
<one-to-many class="c3.data.Project"/>
</set>
</class>
</hibernate-mapping>
As you can see, it holds a relationship to other models called AuthSession and Project. I can post their configuration files if necessary.
The problem I'm having is when I call Session.get(), it never returns; it just hangs. Hibernate shows me the queries, and it runs the query for the User, the AuthSession, and the Projects. Apart from not confirming the values of the parameters, the queries look correct. After the queries for the Projects, nothing happens. I put a print statement right after the call for get and it never gets printed.
I don't know what else to check to see what might be going wrong. Any thoughts?
Edit:
There was an exception, here's the stack trace:
java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2314)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:664)
at org.hibernate.type.EntityType.resolve(EntityType.java:444)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:999)
at org.hibernate.loader.Loader.doQuery(Loader.java:878)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2094)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:678)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1801)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:661)
at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:1014)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:298)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1977)
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:3821)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1075)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:175)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2421)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:971)
at c3.data.UserManager.getUser(UserManager.java:22)
at c3.data.AuthSessionManager.sessionExists(AuthSessionManager.java:14)
at c3.console.Server.logon(Server.java:289)
at c3.console.Server.onToken(Server.java:242)
at tokenwebsocket.server.TokenWebSocketServer.onMessage(TokenWebSocketServer.java:37)
at org.java_websocket.server.WebSocketServer.onWebsocketMessage(WebSocketServer.java:457)
at org.java_websocket.WebSocketImpl.deliverMessage(WebSocketImpl.java:561)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:328)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:149)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:593)
What do I have to change to avoid Hibernate giving me lazy javassist instance proxies rather than the true entity?
UPDATE: I am using Spring 3.x and Hibernate 4.x
The API I am using to load the entity is org.hibernate.internal.SessionImpl#load(Person.class, Id) and the mapping simply:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.perfectjpattern.example.model">
<class name="Person" table="PERSON_" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" update="false" />
<property name="age" update="true" />
</class>
<query name="Person.findByName">
<![CDATA[select p from Person p where p.name = ? ]]>
</query>
<query name="Person.findByAge">
<![CDATA[select p from Person p where p.age = :Age ]]>
</query>
</hibernate-mapping>
Use get() rather than load().
You can use Hibernate.initialize(obj) after session.load(id).
This method can instantly initialize your obj.
Actually solved it by simply changing the mapping to (see the default-lazy="false"):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.perfectjpattern.example.model" default-lazy="false">