- This topic has 7 replies, 3 voices, and was last updated 18 years, 2 months ago by Riyad Kalla.
-
AuthorPosts
-
frankc01aMemberI have a MySQL View with three string columns.
When I reverse engineer for Hibernate, I get the following:
<hibernate-mapping> <class name="com.col.hibernate.Ventypereg" table="ventypereg" catalog="test"> <composite-id name="id" class="com.col.hibernate.VentyperegId"> <key-property name="vendorName" type="java.lang.String"> <column name="VendorName" length="45" /> </key-property> <key-property name="catagory" type="java.lang.String"> <column name="Catagory" length="45" /> </key-property> <key-property name="region" type="java.lang.String"> <column name="Region" length="45" /> </key-property> </composite-id> </class> </hibernate-mapping>
And the following (fundemental) classes are created:
Abstract and concrete Ventypereg.java
VentyperegDAO
Abstract and concrete VentyperegId.javaI want to use query string:
“from Ventypereg as model where model.vendorName like ? AND model.catagory like ? and model. region like ?”But I get the following exception:
org.hibernate.QueryException: could not resolve property: vendorName of: com.col.hibernate.Ventypereg [from com.col.hibernate.Ventypereg as model where model.vendorName like ? AND model.catagory like ? AND model.region = ?]Also, when I stub the code and use VentyperegDAO.findByProperty(…) I get the same exception.
Can someone help here?
Riyad KallaMemberThe problem is that Hibernate cannot manage a table that doesn’t contain a primary key, so what MyEclipse does is uses all the fields of the table to generate a primary key for each record. TO make your life easier, if you add a primary key to this table, and then re-reverse engineer it, your PK will be much simpler than name and the remaining fields will be registered as properties and your query should work fine.
If you don’t do that, I’m not exactly sure how you only query for a part of a primary key, I don’t think you can.
frankc01aMemberThat doesn’t work…it’s a reverse engineered MySQL VIEW which doesn’t support a primary key
Haris PecoMemberfrankc01a,
You can works with view if composite id have unique , non-null values.Tyr with next query :
from Ventypereg as model where model.id.vendorName like ? AND model.id.catagory like ? and model. id.region like ?
I am not sure for findByProperty – Please, send use table script (you can generate one with MyEclipse’s ‘generate-> ddl), POJO and DAO classes
Thanks
Peco
frankc01aMemberThanks, that worked. As far as sending the other stuff, it is a view with three (3) columns that are text (Varchar(45)) from three different tables. None of the columns are primary key.
Haris PecoMemberHibernate request that your id have non-null, unique values.When you have primary key database car about this, but if you use view (or table without PK) , you have to have correct values
for example, if your view (id values) have this :
null,’a’,’b’
it is invalid (null value) or
‘a’,’b’,’c’
‘a’,’b’,’c’is invalid (non-unique values for id)
Best
Peco
frankc01aMemberYes, the view contains unique non-null values. It is working now that I added the “id.” prefix to the attribute in the “model.id.xxx like ?” string.
Thanks for the help
Riyad KallaMemberGlad it’s working.
-
AuthorPosts