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

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.

Related

Hibernate One To Many Relation Hbm Xml Error

I want to create relation between Definition (DEF) and Details (DET) table by foreign keys in Hibernate. In this case, one definition has multiple details.
Definition table has ID column as PK
Details table has ID column as PK and DEFINITION_ID as FK
When I connect these inside of hbm.xml I am getting an error such as;
ERROR
Caused by: org.hibernate.MappingException: Repeated column in mapping
for entity: com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet
column: ID (should be mapped with insert="false" update="false")
DEFINITION HBM XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef"
table="CMS_EXEMPT_RESTRICT_DEF">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DEF</param>
</generator>
</id>
.
.
.
<bag name="cmsExemptRestrictDetails" table="CMS_EXEMPT_RESTRICT_DET"
inverse="true" lazy="true" fetch="select">
<key>
<column name="DEFINITION_ID" not-null="true" />
</key>
<one-to-many
class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet" />
</bag>
</class>
</hibernate-mapping>
DETAIL HBM XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet"
table="CMS_EXEMPT_RESTRICT_DET">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DET</param>
</generator>
</id>
<many-to-one name="cmsExemptRestrictDefinition" class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef" fetch="select">
<column name="ID" not-null="true" />
</many-to-one>
<property name="definitionId" type="java.lang.Long">
<column name="DEFINITION_ID" />
</property>
</class>
</hibernate-mapping>
First - Do I need to define both many to one and one to many relation in each hbm xmls?
Second - Do I need to rename column ID of Detail something like DETAIL_ID?
Thanks.
The property mapping Definition_id is incorrect. The foreign key should not be mapped. Remove it and change the column name in manytoone mapping to definition_id:
Change the detail hbm xml to:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet"
table="CMS_EXEMPT_RESTRICT_DET">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DET</param>
</generator>
</id>
<many-to-one name="cmsExemptRestrictDefinition" class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef" fetch="select">
<column name="DEFINITION_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

Hibernate, updating an object containing a collection without loading the collection

I have a Project that contains a Set of Columns that contains a Set of Tasks that contains a Set of TaskAssigness.
I'm trying to update a column title without having to load its set of tasks, but I get the following error :
[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush()
I have the following hbm.xml :
Project.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.exemple.tasks.project.Project" table="P_TASKS_PROJECT">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<set name="columns" table="P_TASK_PROJECT_COLUMN" inverse="true" cascade="all" sort="com.exemple.tasks.project.ColumnIndexComparator">
<cache usage="read-write" />
<key column="projectId" not-null="true" />
<one-to-many class="com.exemple.tasks.project.Column" />
</set>
</class>
</hibernate-mapping>
Column.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.project.Column" table="P_TASK_PROJECT_COLUMN">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<property name="title" />
<many-to-one name="project" class="com.example.tasks.project.Project">
<column name="projectId" not-null="false" />
</many-to-one>
<set name="tasks" table="P_TASKS_JOIN_COLUMNS">
<cache usage="read-write" />
<key column="columnId" not-null="true" />
<many-to-many column="taskId" class="com.example.tasks.Task" unique="false" />
</set>
</class>
</hibernate-mapping>
Task.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.Task" table="P_TASK">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<set name="taskAssignees" table="P_TASK_ASSIGNEE" cascade="all-delete-orphan">
<cache usage="read-write" />
<key column="taskId" not-null="true" />
<one-to-many class="com.example.tasks.TaskAssignee" />
</set>
</class>
</hibernate-mapping>
TaskAsignee.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.TaskAssignee" table="P_TASK_ASSIGNEE">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<many-to-one name="task" class="com.example.tasks.Task" insert="false" update="false">
<column name="taskId" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
I'm trying to update the title of a column this way :
//Updating column title
Column column = project.getColumn(index); // return a column from the project. Project contains a SortedSet of Column
column.setTitle("NewTitle");
session.saveOrUpdate(project);
This works fine if the column does not contains any tasks.
If I try to update a column containing tasks I get the following error message:
[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush()
If i get tasks and tasks assigness before updating the project, I have no errors :
Set<Task> tasks = col.getTasks();
if (tasks != null) {
for (Task task : tasks) {
task.getTaskAssignees();
}
}
session.saveOrUpdate(project);
Is there a way to change my column title, then update the project without having to get tasks and task assignees ? Can I tell hibernate just to update the column and not try to go further.

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.

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

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! ;)

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>

Categories

Resources