We currently have a working app that uses XML mapping for the objects to be persisted. I want to switch to annotated classes to get away from the external XML mapping files.
Should I just be able to import javax.persistence.* and switch LocalSessionFactoryBean to AnnotationSessionFactoryBean? Or do I actually need to create a separate persistence unit? Don't I already have that, given that everything works with the XML maps? Rather than javax.persistence, should I just be using org.hibernate.annotations?
I guess my confusion may be coming from the fact that I'm been assuming that Annotations and XML maps are alternative methods for describing the persistent classes, all else being equal. Part of it is that we're also using the Spring framework(s).
Here's a simple example...
Code:
/*
* Role.java
*
*/
package com.foo.ncs.acl.model;
import org.acegisecurity.GrantedAuthority;
import java.io.Serializable;
public class Role implements GrantedAuthority, Serializable
{
private long id = 0;
private String role = "";
String description = "";
public Role() {}
public Role(String role, String description)
{
setRole(role);
setDescription(description);
}
public void setId(long rid)
{
this.id = rid;
}
public long getId()
{
return this.id;
}
public void setRole(String role)
{
this.role = role;
}
public String getRole()
{
return this.role;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return this.description;
}
public String getAuthority() {
return getRole();
}
public String toString()
{
return this.role;
}
public boolean equals(Object object)
{
if (! (object instanceof Role))
return false;
Role toCompare = (Role)object;
return this.getId() == toCompare.getId();
}
}
And the matching mapping, Role.hbm.xml
Code:
<?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 default-lazy="false">
<class name="com.foo.ncs.acl.model.Role" table="Role">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="role" column="role" type="string" unique="true" />
<property name="description" column="description" type="string" />
</class>
</hibernate-mapping>
I would think that I could dump the XML mapping file by changing Role.java as follows...
Code:
/*
* Role.java
*
*/
package com.foo.ncs.acl.model;
import org.acegisecurity.GrantedAuthority;
import java.io.Serializable;
import javax.persistence.*;
@Entity (access = AccessType.FIELD)
@Table (name = "Role")
public class Role implements GrantedAuthority, Serializable
{
@Id (generate = GeneratorType.AUTO)
private long id = 0;
@Column (name = "Role")
private String role = "";
@Column (name = "Description")
String description = "";
public Role() {}
public Role(String role, String description)
{
setRole(role);
setDescription(description);
}
public void setId(long rid)
{
this.id = rid;
}
public long getId()
{
return this.id;
}
public void setRole(String role)
{
this.role = role;
}
public String getRole()
{
return this.role;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return this.description;
}
public String getAuthority() {
return getRole();
}
public String toString()
{
return this.role;
}
public boolean equals(Object object)
{
if (! (object instanceof Role))
return false;
Role toCompare = (Role)object;
return this.getId() == toCompare.getId();
}
}
and changing my context.xml file from
Code:
<!-- Hibernate config -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/mcilink/ncs/acl/model</value>
</list>
</property>
</bean>
to
Code:
<!-- Hibernate config -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3. AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/mcilink/ncs/acl/model</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>Role</value>
</list>
</property>
</bean>
-David
PS. I may have some typos above.