-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Converting hbm.xml to use annotations
PostPosted: Tue Aug 26, 2008 1:53 pm 
Newbie

Joined: Tue Aug 26, 2008 12:30 pm
Posts: 2
Can someone help? I am sure my ManyToOne annotation is not correctly defined. I need help in converting these hbm.xml maps to Annotated objects. If i remove all annotations and revert back to using the hbn.xml files it works just fine

Hibernate version:
[Version] Hibernate Annotations 3.3.0.GA
[Environment] Hibernate 3.2.5

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi I am trying to convert the following hbm.xml to use annotations

Employee.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="lab.vo.Employee" table="employee">
<id
name="employeeId"
type="integer"
column="employeeid"
length="10"
>
<generator class="native" />
</id>
<property
name="age"
type="integer"
column="age"
length="10"
/>
<property
name="firstName"
type="string"
column="firstname"
length="256"
/>
<property
name="lastName"
type="string"
column="lastname"
length="256"
/>
<!-- also can configure hibernate lazy here lazy="false"
to hql it's not necessary-->
<many-to-one name="department" column="departmentid" class="lab.vo.Department"/>
</class>
</hibernate-mapping>

------------------------------------------------------------------------------
Department.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="lab.vo.Department" table="department">
<id
name="departmentId"
type="integer"
column="departmentid"
length="10"
>
<generator class="native" />
</id>
<property
name="name"
type="string"
column="name"
length="256"
/>
</class>
</hibernate-mapping>

-----------------------------------------------------------------------
Here is my converted objects using annotations

Employee.java

package lab.vo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import javax.persistence.Table;

@Entity
@Table (name="employee")
public class Employee implements Serializable {
@Id
@GeneratedValue
@Column(name = "employeeid")
private Integer employeeId;
private Integer age;
private String firstName;
private String lastName;

@ManyToOne( cascade = {CascadeType.ALL}, fetch=FetchType.EAGER,targetEntity = lab.vo.Department.class, optional=false)
@JoinColumn(name="departmentid")
private Department department;

public Employee() {
}

public Employee(Integer employeeId, String firstName, String lastName, Integer age, Department department) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.department = department;
System.out.println("Department set "+department.name);
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
System.out.println("Department set "+department);
if (department !=null)
System.out.println("Department set "+department.name);

this.department = department;
}

public Integer getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}

----------------------------------------------------------------------------
Department.java
package lab.vo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table (name="department")
public class Department implements Serializable {
@Column(name="departmentid")
Integer departmentid;
String name;

public Department() {
}

public Department(Integer departmentId, String name) {
this.departmentid = departmentId;
this.name = name;
}

@Id
@GeneratedValue
public Integer getDepartmentId() {
return departmentid;
}

public void setDepartmentId(Integer departmentId) {
System.out.println("*Dept ID set to :"+departmentId);
this.departmentid = departmentId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

------------------------------------------------------------------------
hibernate.cfg.xml contents:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/xdb</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<mapping class="lab.vo.Employee"/>
<mapping class="lab.vo.Department"/>
</session-factory>
</hibernate-configuration>

----------------------------------------------------
JBoss 4.2 reports it has binded the objects

12:15:51,859 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
12:15:51,859 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
12:15:51,859 INFO [Configuration] Configured SessionFactory: null
12:15:51,859 INFO [AnnotationBinder] Binding entity from annotated class: lab.vo.Employee
12:15:51,859 INFO [EntityBinder] Bind entity lab.vo.Employee on table employee
12:15:51,875 INFO [AnnotationBinder] Binding entity from annotated class: lab.vo.Department


The issue is when i try and create a new Employee record, it creates the employee record but fails to create the Department it is assocaited with. No errors are reported. If i revert back to the hbm.xml files it works fine. The Employee table stores the departmentid key which is key for Department table

---------------------------------------------------------------------------
Here is the JSP page used

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<link href="<s:url value="/styles/main.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>
<center>
<div class="titleDiv"><s:text name="application.title"/></div>
<h1>
<s:if test="employee==null || employee.employeeId == null">
<s:text name="label.employee.add"/>
</s:if>
<s:else>
<s:text name="label.employee.edit"/>
</s:else>
</h1>
<table width=600 align=center>
<tr><td><a href="getAllEmployees.action">Click Here to View Employees</a></td>
</tr>
</table>
<table>
<tr><td align="left" style="font:bold;color:red">
<s:fielderror/>
<s:actionerror/>
<s:actionmessage/></td></tr>
</table>
<s:form>
<table align="center" class="borderAll">

<tr><td class="tdLabel"><s:text name="label.firstName"/></td>
<td><s:textfield name="employee.firstName" size="30"/></td>
</tr>
<tr>
<td class="tdLabel"><s:text name="label.lastName"/></td>
<td><s:textfield name="employee.lastName" size="30"/></td>
</tr>
<tr><td class="tdLabel"><s:text name="label.age"/></td>
<td><s:textfield name="employee.age" size="20"/></td>
</tr>
<tr>
<td class="tdLabel"><s:text name="label.department"/></td>
<td><s:select name="employee.department.departmentid"
list="#session.departments"
listKey="departmentid"
listValue="name"
/>
</td>
<s:hidden name="employee.employeeId"/>
</tr>
</table>
<br/>
<table>
<tr>
<td><s:submit action="insertOrUpdate" key="button.label.submit" cssClass="butStnd"/></td>
<td><s:reset key="button.label.cancel" cssClass="butStnd"/></td>
<tr>
</table>
</s:form>
</center>
</body>
</html>

--------------------------------------------------------------------------
EmployeeAction

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<link href="<s:url value="/styles/main.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>
<center>
<div class="titleDiv"><s:text name="application.title"/></div>
<h1>
<s:if test="employee==null || employee.employeeId == null">
<s:text name="label.employee.add"/>
</s:if>
<s:else>
<s:text name="label.employee.edit"/>
</s:else>
</h1>
<table width=600 align=center>
<tr><td><a href="getAllEmployees.action">Click Here to View Employees</a></td>
</tr>
</table>
<table>
<tr><td align="left" style="font:bold;color:red">
<s:fielderror/>
<s:actionerror/>
<s:actionmessage/></td></tr>
</table>
<s:form>
<table align="center" class="borderAll">

<tr><td class="tdLabel"><s:text name="label.firstName"/></td>
<td><s:textfield name="employee.firstName" size="30"/></td>
</tr>
<tr>
<td class="tdLabel"><s:text name="label.lastName"/></td>
<td><s:textfield name="employee.lastName" size="30"/></td>
</tr>
<tr><td class="tdLabel"><s:text name="label.age"/></td>
<td><s:textfield name="employee.age" size="20"/></td>
</tr>
<tr>
<td class="tdLabel"><s:text name="label.department"/></td>
<td><s:select name="employee.department.departmentid"
list="#session.departments"
listKey="departmentid"
listValue="name"
/>
</td>
<s:hidden name="employee.employeeId"/>
</tr>
</table>
<br/>
<table>
<tr>
<td><s:submit action="insertOrUpdate" key="button.label.submit" cssClass="butStnd"/></td>
<td><s:reset key="button.label.cancel" cssClass="butStnd"/></td>
<tr>
</table>
</s:form>
</center>
</body>
</html>

--------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 5:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

first of all I recommend you to turn on debug trace and also set show_sql to true. This will give you more information about the detected mappings and you will see the executed sql.

Regarding your mapping - you are mixing placing the annotations on getters and properties. You should do one or the other consistently. There is a difference between them since the JPA provider has to decide whether to use direct properties access or getter access.

Let me know if the problem is still around once you consistently placed your annotations.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.