-->
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: Baffled re: switch from XML mapping to Annotations
PostPosted: Tue Aug 14, 2007 2:51 pm 
Newbie

Joined: Fri Aug 10, 2007 9:40 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 11:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, aside from the Spring configuration that I don't grok, you should be able to do it (ie replacing the HBM XML metadata with annotations, the rest being fine).

_________________
Emmanuel


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.