I'm using multiple hibernate named queries that maps to the same hibernate class.
The problem is because the different named queries return different attributes, and not all the attributes I'm getting an "invalid column" error. The way to resolve it is to make sure the queries map all the attributes defined by the hibernate class. (Hibernate native query : Invalid Column Name Error SQL-17006)
Here's an sample of what I have:
<hibernate-mapping>
<class name="com.company.ObjectA" mutable="false" >
<id name="id" type="string"/>
<property name="prop1" type="string"/>
<property name="prop2" type="string"/>
</class>
<sql-query name="get1">
<return alias="a" class="com.company.ObjectA"/>
<![CDATA[
select
id as {a.id},
prop1 as {a.prop1}
from TABLE_A
]]>
</sql-query>
<sql-query name="get2">
<return alias="a" class="com.company.ObjectA"/>
<![CDATA[
select
id as {a.id},
prop2 as {a.prop2}
from TABLE_A
]]>
</sql-query>
</hibernate-mapping>
I have defined a temporary fix where the queries return empty values to make sure all hibernate class attributes are mapped:
<sql-query name="get2">
<return alias="a" class="com.company.ObjectA"/>
<![CDATA[
select
id as {a.id},
'' as {a.prop1},
prop2 as {a.prop2}
from TABLE_A
]]>
</sql-query>
But that is not ideal, because for my real case, I have quite a few of these unmapped attributes, and more named queries that use a different combination of attributes in the hibernate class.
Is there another way to resolve it? Also I need it to return data to the same object 'ObjectA', because of it's integration with the rest of the code.
Related
I dont know why but when I import this project to Eclipse. This work well.
So, I think this is problem of eclipse project when import to InteliJ IDEA
This not easy such my imagine.
I have class Setting and Setting.hbm.xml for mapping hibernate.
In this class:
<hibernate-mapping>
<class name="Setting" table="setting" lazy="false">
<id name="id" column="id" type="integer">
<generator class="increment" />
</id>
.....
</class>
<query name="select.setting">
from Setting as s where s.id = ? order by s.name
</query>
Now, when I call function
this.getHibernateTemplate().findByNamedQuery("select.setting", params);
This return error
org.springframework.orm.hibernate4.HibernateSystemException: Named query not known: select.setting; nested exception is org.hibernate.MappingException: Named query not known: select.setting
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:218) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:343) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:933) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
Please give advice about it.
You can give a try with this.
<query name="select.setting">
<![CDATA[from Setting as s where s.id = ? order by s.name]]>
</query>
The XML parser gets confused of you are not using CDATA tag.
CDATA is way of telling the framework that its a data which should not be interpreted as a markup.
Hence as #Lovababu mentioned, include the query inside CDATA tags:
<query name="select.setting">
<![CDATA[from Setting as s where s.id = ? order by s.name]]>
<sql-query name="sql">
select :COLUMN_NAME from table.
</sql-query>
I want to set COLUMN_NAME using
this.sessionFactory.getCurrentSession().getNamedQuery("sql")
.setString("COLUMN_NAME", "id");
I got a wrong result when I use this code (I know where I fail). Is there any way to set COLUMN_NAME using getNamedQuery() and setString().
Have you tried setParameter method?
this.sessionFactory.getCurrentSession().getNamedQuery("sql")
.setParameter("COLUMN_NAME", "id");
Further info
You must add <return-scalar column="name_first" type="string" /> to get specific column. like
<sql-query name="sql">
<return-scalar column="COLUMN_NAME" type="java.lang.String" />
select :COLUMN_NAME from table.
</sql-query>
Or you can use setResultTransformer method if you had created a bean class for that table.
I am new to Hibernate and was writing some test program.
I am wondering if its a must to have a table , one column of which will be updated using some kind of sequence.
For ex. I created a table
create table course(course_name varchar2(20));
and when I am defining Course.hbm.xml in the following way
<class name="Course" table="COURSE" >
<property name="course">
<column name="course"/>
</property>
</class>
I am getting an error in the XML file saying a declaration of "id" or something similar is expected. I can give the whole error message if required.
You need an ID column so hibernate can identify that row in the table. I'm not fluent in that oldschool hibernate xml mapping but it should look roughly like that:
create table course(id integer primary key, course_name varchar2(20));
<class name="Course" table="COURSE" >
<id name="id">
<!-- uses sequence, auto increment or whatever your DBMS uses for id generation -->
<generator class="native"/>
</id>
<property name="course">
<column name="course"/>
</property>
</class>
As a side note: mapping your entities with annotations is a bit more common nowadays. Makes it easier, especially for starters.
Is there way to set results of a query to java DTO property using hibernate ?
Something like,
<hibernate-mapping>
<class name="myDTO" table="my_table">
<property name="myProperty" query="what_i_need" />
</class>
<query name="what_i_need">
<![CDATA[
from .....
]]>
</query>
</hibernate-mapping>
I'm using spring, hibernate, java.
If I understand you correctly #josef-prochazka 's suggestion of using a formula would be what you need.
See also:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-column
http://www.onjava.com/pub/a/onjava/2005/08/03/hibernate.html?page=2
http://www.java2s.com/Code/Java/Hibernate/ColumnFormulaStringConcatenate.htm
In my Java applicaion, I am using hibernate .hbm file to access database; Is this possible to update the primary key 'id' column in the table; Where the 'id' column in my .hbm file is like:
<hibernate-mapping package="org.jems.user.model">
<class name="Student_Details" table="t_student">
<id name="id" type="int" column="id">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string" unique="true" not-null="true" />
<property name="description" column="description" type="string" />
<property name="comments" column="comments" type="string" />
<property name="active" column="isActive" type="boolean" not-null="true" />
</class>
</hibernate-mapping>
try this:
String hql="update table set id=? where id=? ";
Query query=HibernateSessionFactory.getSession().createQuery(hql);
query.setInteger(0,1);
query.setInteger(1,2);
query.executeUpdate();
HibernateSessionFactory.getSession().beginTransaction().commit();
or just use sql:
String sql = "update table set id = ? where id= ?"
Session session = HibernateSessionFactory.getSession();
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 1);
query.setParameter(1, 2);
No. Hibernate doesn't allow to change the primary key. In general, a primary key value should never change, if needs to be changed than the primary key column(s) are not good candidate(s) for a primary key.
There is a workaround if you prefer to update via entity instead of query:
1) clone the entity to a new entity.
2) delete the old entity. (be careful of your cascade children entities)
3) change the primary key of new entity (or set it null depend your generation strategy).
4) save the new entity.
In hibernate id column values are automatically incremented while session.save() is executed based on which generation strategy you use. Check this post for a simple example
try to writing query like
update table_name set id=value where...............(specify remaining conditions)
Basically, Hibernate does not allow to update or change the primary key of a database entity.
The reason is the entity data that you fetch from database via some query or .get or .load method goes into persistent context layer.
So from hibernate perspective updating of such persistent entity object means deletion of the older one from database and creation of a new one.
It better to make normal update query like
Query query=HibernateSessionFactory.getSession().createQuery(update table set id=? where id=?);
query.executeUpdate();
HibernateSessionFactory.getSession().beginTransaction().commit();