hibernate generate pojo class with field name = data base column name - java

When I use hibernate eclipse plugin to generate java class from my table, the private field name is the same with my table name. Is there a way for me to make it to be the same with my column name instead?
For example:
table: User
column : name (string)
column : homeAddressId (fk to addressTable)
The generated class right now is:
User{
private String name;
private Address address;
}
I want it to be:
User{
private String name;
private Address homeAddress;
}
Is there a settings to do that?
Thanks very much for your help.

there can be problem in the understanding between you and hibernate. Actually, what i see, there is a foreign key with other table which is Address, not homeAddress.
can you also post the other table(Address) structure.
as if the other table name is Address then hibernate plugin is bond to create create attribute with name address, as i feel plugin doesn't have intelligence of its own.

Before anything, your domain should be the way you want it. If you need to make more changes to the mapping (ORM), you might need to do it in your hibernate-conf.xml. The generated class and the current class is the same. For example the user.xml will be something like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="ADMIN_USER_TBL">
<id name="id" type="string" column="ADMIN_USER_ID">
<generator class="assigned" />
</id>
<property name="name" column="FIRST_NAME" />
...
</class>
</hibernate-mapping>
Then, have the mapping for the address:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Address" table="ADDRESS_TBL">
<id name="id" type="string" column="ADMIN_USER_ID">
<generator class="assigned" />
</id>
<property name="address" column="ADDRESS1" />
<property name="address2" column="ADDRESS2" />
<property name="city" column="ADDRESS_CITY" />
<property name="state" column="ADDRESS_STATE" />
<property name="zip" column="ADDRESS_ZIP" />
...
</class>
</hibernate-mapping>
Let me know if that helps! ;)

Related

Criteria - where clause on joined table

I have two tables in a MySQL database: COURSE and COURSE_SESSION.
COURSE has two fields : code(primary key) and title.
CourseSession has a field called course_code which is a foreign key linked to an element of Course.
I want to do somthing like this but using the Criteria API in Java :
SELECT * FROM COURSE_SESSION join COURSE
WHERE course_code = code AND title like "%substring%";
Both tables are mapped correctly with hibernate.
I tried this :
Criteria criter = s.createCriteria(CourseSession.class);
criter.add(Restrictions.like("courseCode.title", "%substring%"));
But I end up with an error :
HTTP 500 - could not resolve property: courseCode.title of: org.xxx.core.entity.CourseSession
message could not resolve property: courseCode.title of: org.xxx.core.entity.CourseSession
exception
org.hibernate.QueryException: could not resolve property: courseCode.title of: fr.utbm.lo54.core.entity.CourseSession
org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96)
org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1443)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:443)
org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:114)
org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:83)
org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:92)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
fr.utbm.lo54.core.servlet.SearchData.doGet(SearchData.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
The strange thing is that if I do a Restriction on courseCode.code with Restrictions.like("courseCode.code", "%substring%"), it works fine.
EDIT
Course.hbm.xml :
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.xxx.core.entity">
<class name="Course" table="COURSE">
<id name="code" column="CODE">
<generator class ="identity"/>
</id>
<property name="title" column="TITLE" not-null="true"/>
</class>
</hibernate-mapping>
CourseSession.hbm.xml :
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.xxx.core.entity">
<class name="CourseSession" table="COURSE_SESSION">
<id name="id" column="ID">
<generator class="identity" />
</id>
<property name="startDate" column="START_DATE" not-null="true" />
<property name="endDate" column="END_DATE" not-null="true" />
<many-to-one name="courseCode" column="COURSE_CODE" />
<many-to-one name="locationId" column="LOCATION_ID" />
</class>
</hibernate-mapping>
create an alias for the field first to achieve this.
try the below code, it should help
criter.createAlias("courseCode","courseCode");//course code is the association variable in your pojo class.

HQL JOIN QUERY: could not resolve property:

I am getting the following error when I try to execute a join query
("could not resolve property: Countries of: com.fexco.helloworld.web.model.Customer [select cus from com.fexco.helloworld.web.model.Customer as cus join cus.Countries as cou where cus.id = cou.id]")
I am trying to join the Customer and Countries tables together by a common id
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ccg.db.test">
<class name="Customer" table="Customer">
<id name="id" column="id" type="bigiint">
<generator class="native"/>
</id>
<property name="firstname" type="string" >
<column name="firstname" />
</property>
<property name="surname" type="string" >
<column name="surname" />
</property>
<property name="address1" type="string" >
<column name="address1" />
</property>
<property name="address2" type="string" >
<column name="address2" />
</property>
<many-to-one name="Countries" column="id" class="Countries" />
</class>
</hibernate-mapping>
Countries.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ccg.db.test">
<!-- CLASS NAME MIGHT BE CUSTOMER -->
<class name="Countries" table="Countries">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="country" column="country" />
</class>
</hibernate-mapping>
And this is the query that i am trying to call
String sql_query = "select cus from Customer as cus join cus.Countries as cou where cus.id = cou.id";
I am new to HQL so not sure about everything with it yet so does anybody know how to solve this?
Thanks
It's cus.countries, not cus.Countries. Property names are case sensitive.
try
select cus from Customer cus where cus.id = cus.countries.id

How to cleanly map 2 one-to-one associations in Hibernate

I am currently trying to create two simple one-to-one mappings using Hibernate but somehow it doesn't work as I want it to.
My primary class is called MailAccount and its mapping looks like this:
<?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">
<!-- Generated 26.04.2011 14:49:15 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping package="test.account">
<class name="MailAccount" table="MAILACCOUNTS" dynamic-update="true">
<id name="id" column="MAIL_ACCOUNT_ID">
<generator class="native" />
</id>
<one-to-one name="incomingServer" cascade="all" />
<one-to-one name="outgoingServer" cascade="all" />
</class>
</hibernate-mapping>
The server mapping file looks like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 02.05.2011 12:32:52 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="test.server.MailServer" table="MAILSERVER">
<id name="id" type="long" access="field">
<column name="MAIL_SERVER_ID" />
<generator class="native" />
</id>
<one-to-one name="mailAccount" class="test.account.MailAccount" foreign-key="MAIL_SERVER_ID"></one-to-one>
</class>
</hibernate-mapping>
Now, if I let Hibernate create the tables, I get pretty much that what I want: A table "MailAccount" with the column "MAIL_ACCOUNT_ID" and another table "MailServer" also with a column for the id.
If I call session.save(mailAccountInstance);, Hibernate correctly saves the data to the tables.
BUT once I try to load the data into a MailAccount instance, Hibernate only loads the "incomingServer" property into a new MailAccount instance and the outgoingServer property is empty.
I also don't understand how Hibernate joins both tables together since the table "MailServer" doesn't save the id of the MailAccount each server belongs to as a foreign key.
How can I fix this?
Thanks in advance!
Ps: I am pretty new to Hibernate, so don't beat me up to much for obvious mistakes :-)
I suggest taking a look at the Association Mappings chapter in the Hibernate reference manual. For a bidirectional one-to-one mapping, it's suggested to use a <one-to-one> on one end and <many-to-one unique="true"> on the other end.

Hibernate+Spring framework project mapping error(exception)

Caused by:
org.hibernate.MappingException: Could not determine type for: controler.Role, for columns: [org.hibernate.mapping.Column(ROLE)]
Can you please help me on this?
this is my mapping class
<?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="controler.Role" table="ROLE">
<id name="roleId" column="ROLEID">
<generator class="increment"/>
</id>
<property name="title" column="TITLE"/>
</class>
</hibernate-mapping>
the Role is a pojo class and i have the relevant table named Role in the JavaDB. The role table has attributes roleid(char) and roletitle(varchar)
Caused by: org.hibernate.MappingException: Could not determine type for: controler.Role, for columns: [org.hibernate.mapping.Column(ROLE)]
My initial assumption was wrong. But now that you mentioned JavaDB, I suspect that ROLE is actually a reserved keyword. Try to enclose the table name in backticks in the mapping document:
<?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="controler.Role" table="`ROLE`">
<id name="roleId" column="ROLEID">
<generator class="increment"/>
</id>
<property name="title" column="TITLE"/>
</class>
</hibernate-mapping>
References
Hibernate reference guide
5.4. SQL quoted identifiers
Is the fully-qualified name of the Java class actually controler.Role? What does the source code of the Role class look like? Is it in a package named controler?
Perhaps the name is simply misspelt.
<?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="controler.Role" table="ROLE">
<id name="roleId" type="int" column="ROLEID">
<generator class="increment"/>
</id>
<property name="title" column="TITLE"/>
</class>
</hibernate-mapping>

Hibernate unidirectional one-to-many.i'm a bit confused

Hello guys sorry if the question looks stupid to you.
i have 3 tables currency (id | name) language (id| name | description) transaction (id|amount|languageId | currencyid)
so i want to insert into the transaction but making sure that it doesn't insert unknown language or currency (meaning it shouldn't insert to messagetemplate if there is no existing parent language and currency)
here are my mapping files
<?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 package="com.myproject.model">
<class name="Transaction" table="transaction">
<id name="id">
<generator class="native"/>
</id>
<property column="amount" name="amount" type="String"/>
<many-to-one class="CurrencyImpl" column="currency" name="currency"/>
<many-to-one class="LanguageImpl" column="language" name="language"/>
</class>
</hibernate-mapping>
<?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 package="com.myproject.model">
<class name="Currency" table="currency">
<id name="id">
<generator class="native"/>
</id>
<property column="currency_name" name="name" type="String"/>
</class>
</hibernate-mapping>
<?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 package="com.myproject.model">
<class name="Language" table="language">
<id name="id">
<generator class="native"/>
</id>
<property column="language_name" name="name" type="String"/>
<property column="language_description" name="description" type="String"/>
</class>
</hibernate-mapping>
with this current mapping it's seems not to be the case.how to achieve that? thanks a lot for reading
You're many to one references CurrencyImpl and LanguageImpl, but those classes are not mapped, only the (presumably corresponding) interfaces. I suggest you begin by creating and mapping only concrete classes and get that working before trying to mess about with mapping interfaces.

Categories

Resources