Mapping Exception in hibernate - java

I'm trying to implement one-to-one association mapping but it gives MappingException
Student.hbm.xml file:
<?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 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Student" table="STUDENT">
<id name="studentId" type="java.lang.Long">
<column name="STUDENT_ID" />
<generator class="native" />
</id>
<property name="studentName" type="string" length="20" column="STUDENT_NAME"/>
<many-to-one name="studentAddress"
column="STUDENT_ADDRESS" class="com.sst.student.Address" cascade="all"
unique="true"/>
</class>
</hibernate-mapping>
my Address.hbm.xml file:
<?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 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Address" table="ADDRESS">
<id name="addressId" type="java.lang.Long">
<column name="ADDRESS_ID" />
<generator class="native" />
</id>
<property name="street" type="string" column="STREET" length="20"/>
<property name="city" type="string" column="CITY" length="20"/>
<property name="state" type="string" column="STATE" length="20"/>
<property name="country" type="string" column="COUNTRY" length="20"/>
</class>
</hibernate-mapping>
at the time of code generation I'm getting the following exception:
org.hibernate.MappingException: An association from the table STUDENT refers to an
unmapped class: com.sst.student.Address
my cfg file "one2one.cfg.xml":
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root$12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306
/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/sst/student/Student.hbm.xml"/>
<mapping resource="com/sst/student/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>

You need a one-to-many mapping in Address hbm.xml.

Related

Hibernate mapping - The content of element type "class" must match "(meta*,subselect

I've a problem with the mapping of a class on hibernate...
I've already checked on the web, but i don't find the same problem
This is the error:
The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|
composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-
component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-
subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
and this is my code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="it.****.extractor.application.DGEPRA" table="T_DGEPRA_LOG">
<id name="id" type="int" column="ID"/>
<property name="time" column="TIME" type="timestamp"/>
<property name="function" column="FUNCTION" type="string"/>
<property name="executionTime" column="EXECUTION_TIME" type="int"/>
<property name="guId" column="GUID" type="string"/>
</class>
</hibernate-mapping>
any idea?

Invalid Mapping Exception in hibernate.hbm.xml

I am posting my hbm file with the cfg file along with my error log. I found my questions based on similar problems are tried out their solution too but still my error is not fading away.
log
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.viralpatel.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.viralpatel.hibernate.Main.main(Main.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
... 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more
Caused by: org.dom4j.DocumentException: Error on line 9 of document : The element type "generator" must be terminated by the matching end-tag "</generator>". Nested exception: The element type "generator" must be terminated by the matching end-tag "</generator>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 10 more
Employee.hbm.xml
<?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="Employee" table="EMPLOYEE" >
<id name="employeeId" column="EMPLOYEE_ID">
<generator class="native">
</id>
<one-to-one name="employeeDetail" class="net.viralpatel.hibernate.EmployeeDetail"
cascade="save-update"></one-to-one>
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />
</class>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">tcs#1234</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="net/viralpatel/hibernate/EmployeeDetail.hbm.xml"/>
<mapping resource="net/viralpatel/hibernate/Employee.hbm.xml"/>
</session-factory>
In your Employee.hbm.xml you're not closing the generator tag.
Replace
<generator class="native">
by this
<generator class="native"/>
By looking at stack trace, you have not end the generator tag properly.
Please change your employee.hbm.xml file to end the <generator> element properly.
For Ex: It should be <generator class="native"/>

Java hbm.xml multiple index of one column

For example,
<?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.sql.index">
<class name="User">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="firstName" type="string" index="IDX_FIRST_NAME" />
<property name="lastName" type="string" />
<property name="address" type="string" />
<property name="field_1" type="long" />
<property name="field_2" type="long" />
</class>
</hibernate-mapping>
If I want the field_1 and field_2 has 2 indexes description. Can I do the following thing? Or How to achieve it ?
<property name="field_1" type="long" index="idx_1,idx_2"/>
<property name="field_2" type="long" index="idx_1,idx_3"/>
The field_1 and field_2 will has 2 index for their self.
I refer the hibernate 3.6, 5.1.4.2 Property mapping with hbm.xml, it seems like the index field can be assigned by only one column.
PS,
The project is some kind old, and is maintained by many people, so I cannot use annotation syntax to add index.
I found the post and give it a try.
<?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.sql.index">
<class name="User">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="firstName" type="string" index="IDX_FIRST_NAME" />
<property name="lastName" type="string" />
<property name="address" type="string" />
<property name="field_1" type="long" index="idx_2"/>
<property name="field_2" type="long" index="idx_3"/>
</class>
<database-object>
<create>
CREATE INDEX idx_1 ON User (field_1, field_2)
</create>
<drop></drop>
</database-object>
</hibernate-mapping>
This problem can be solved by <database-object>, by writing native sql syntax to create index.
UPDATE, 2018/11
For unique constraint with multiple properties, someone has answered it.
Use properties tag
<properties name="uk1" unique="true">
<property name="username" .../>
<many-to-one name="client" .../>
</properties>
<properties name="uk2" unique="true">
<property name="email" .../>
<many-to-one name="client" update="false" insert="false" .../>
</properties>

Hibernate basic configuration in Spring 3 project - Class not found exception

I'm trying to set up Hibernate 3.6 in my existing Spring 3 MVC + Spring 3 security project. Nevertheless I keep getting java.lang.ClassNotFoundException in my HibernateUtil class - line with return new Configuration().configure().buildSessionFactory();
My project structure looks like this:
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3049/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="./DBUser.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
DBUser.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>
<class name="../src/main/java/user/DBUser" table="DBUSER">
<id name="userId" type="int">
<column name="USER_ID" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="20" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="20" not-null="true" />
</property>
<property name="createdDate" type="date">
<column name="CREATED_DATE" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>
And a relevant part of the pom.xml file:
<!-- Hibernate + MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
EDIT: I've pasted here the stack trace. It was too long to put it right here, because it would make this post confused.
I'd be really glad, if someone could help me with setting up this thing, because I've got no idea, why it can't find that class.
It looks like you are missing the javaassist library. Download the javaassist jar and add it to your classpath.
Your class name should be the package path to the Hibernate POJO in DBUser.hbm.xml, e.g:
user/DBUser
And your mapping resource does not need ./ in the path.

Relationship set between table and mapping table to use joins

I have two tables: a "Module" table and a "StaffModule" table. I want to display a list of modules by which staff are present on the staffmodule mapping table.
I've tried
from Module join Staffmodule sm with ID = sm.MID
with no luck, I get the following error
Path Expected for join!
However, I thought I had the correct join too allow this, but this is obviously not the case. Can any one help?
StaffModule HBM
<?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">
<!-- Generated Apr 26, 2010 9:50:23 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Hibernate.Staffmodule" schema="WALK" table="STAFFMODULE">
<composite-id class="Hibernate.StaffmoduleId" name="id">
<key-many-to-one name="mid" class="Hibernate.Module">
<column name="MID"/>
</key-many-to-one>
<key-property name="staffid" type="int">
<column name="STAFFID"/>
</key-property>
</composite-id>
</class>
</hibernate-mapping>
and Module.HBM
<?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">
<!-- Generated Apr 26, 2010 9:50:23 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Hibernate.Module" schema="WALK" table="MODULE">
<id name="id" type="int">
<column name="ID"/>
<generator class="assigned"/>
</id>
<property name="modulename" type="string">
<column length="50" name="MODULENAME"/>
</property>
<property name="teacherid" type="int">
<column name="TEACHERID" not-null="true"/>
</property>
</class>

Categories