I'm just scratching the surface of Hibernate at the moment and I'm
running into problems.
I'm trying it out with Eclipse, Hibernate Synchronizer and MySQL and
things are vaguely working I think - although I can't seem to get
Middlegen to work with this environment due to a seeming lack of
documentation.
Anyway - I've got this table trio - login, supplier and country. A
login can have one supplier and one country; the DDL for these is
shown below....
create table login (
login_id int NOT NULL AUTO_INCREMENT,
login_cd char(16),
login_nm varchar(80),
email_address varchar(80),
pwd varchar(16),
login_count int,
country_id int,
supplier_id int,
city_nm varchar(255),
position_desc varchar(255),
admin_fl char(1),
version int,
update_dt datetime,
primary key(login_id),
foreign_key(supplier_id) references supplier(supplier_id),
foreign_key(country_id) references country(country_id)
);
create table country (
country_id int NOT NULL AUTO_INCREMENT,
country_cd char(16),
country_nm varchar(255),
version int,
update_dt datetime,
primary key(country_id)
);
create table supplier (
supplier_id int NOT NULL AUTO_INCREMENT,
supplier_cd char(16),
supplier_nm varchar(80),
supplier_logo_url varchar(255),
supplier_desc text,
supplier_url varchar(255),
supplier_contact_email varchar(80),
submitter_email varchar(80),
submitter_name varchar(80),
submitter_login_id int,
version int,
update_dt datetime,
primary key(supplier_id)
);
I've been able to Hibernate Synchronize these tables using hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.mzuzu.test.hibernate.mapping">
<class name="Login" table="login">
<id name="LoginId" type="integer" unsaved-value="null">
<column name="login_id" sql-type="integer" not-null="true"/>
<generator class="native"/>
</id>
<one-to-one name="country"
class="com.test.hibernate.mapping.Country" constrained="true"/>
<property column="position_desc" length="255" name="PositionDesc"
not-null="false" type="string"/>
<many-to-one name="supplier" column="supplier_id"
class="com.test.hibernate.mapping.Supplier"/>
<property column="login_cd" length="16" name="LoginCd"
not-null="false" type="string"/>
<property column="city_nm" length="255" name="CityNm"
not-null="false" type="string"/>
<property column="login_count" length="11" name="LoginCount"
not-null="false" type="integer"/>
<property column="login_nm" length="80" name="LoginNm"
not-null="false" type="string"/>
<property column="admin_fl" length="1" name="AdminFl"
not-null="false" type="string"/>
<property column="update_dt" length="19" name="UpdateDt"
not-null="false" type="timestamp"/>
<property column="version" length="11" name="Version"
not-null="false" type="integer"/>
<property column="pwd" length="16" name="Pwd" not-null="false"
type="string"/>
</class>
</hibernate-mapping>
... I'm not totally confident of the impact of the one-to-one and
many-to-one tags - but this doesn't seem to be an issue at the moment.
The problem I have is that I've got a getCountryByCd(..) method in my
generated CountryDAO (see below) .....
public class CountryDAO extends BaseCountryDAO {
private static String BY_CD = "where country_cd = :country_cd";
public static Country getCountryByCd(String code) throws
HibernateException {
Session session = _RootDAO.createSession();
try {
Query query = session.getNamedQuery(BY_CD);
query.setString("country_cd", code);
return (Country)query.list().get(0);
}
finally {
session.close();
}
}
}
.... that when I run it gives me a message that states :-
.
.
.
.
INFO: starting update timestamps cache at region:
net.sf.hibernate.cache.UpdateTimestampsCache
17-Aug-2004 16:31:51 net.sf.ehcache.config.Configurator configure
WARNING: No configuration found. Configuring ehcache from
ehcache-failsafe.xml found in the classpath:
jar:file:/C:/Java/hibernate-2.1/lib/ehcache-0.7.jar!/ehcache-failsafe.xml
17-Aug-2004 16:31:51 net.sf.ehcache.hibernate.Plugin <init>
WARNING: Could not find configuration for
net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the
defaultCache settings.
17-Aug-2004 16:31:51 net.sf.hibernate.cache.QueryCache <init>
INFO: starting query cache at region:
net.sf.hibernate.cache.QueryCache
17-Aug-2004 16:31:51 net.sf.ehcache.hibernate.Plugin <init>
WARNING: Could not find configuration for
net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache
settings.
main Exception net.sf.hibernate.MappingException: Named query not
known: where country_cd = :country_cd
I cannot find any mention of this "Named query not known" or why it
might come out. Has anyone got any suggestions where I might look for
a solution?
Many thanks
Alan
Alan - 20 Aug 2004 14:46 GMT
Think I found the solution, looking at the Hibernate private forums on
http://forum.hibernate.org/viewforum.php?f=1 ... Named Queries are
declared in the .hbm file with the format...
<query name="country.by_code">
<![CDATA[
from com.test.hibernate.mapping.Country where country_cd =
:country_cd]]>
</query>
I then call
try {
Query query = session.getNamedQuery("country.by_code");
query.setString("country_cd", code);
return (Country)query.list().get(0);
}
finally {
session.close();
}
Thanks anyway
Alan - 24 Aug 2004 13:20 GMT
... looking into the http://forum.hibernate.org forum I think I've
found the clues... named queries have to be declared in the .hbm
files, e.g.
<query name="login.by_code">
<![CDATA[
from com.test.hibernate.mapping.Login where login_cd = :login_cd
]]>
</query