Basically, there are two ways you can define an entity POJO:
* At the getter-methods level
* At the object's properties level
The following example shows you how you can create an entity bean named MyObjectVO:
Entity bean with annotations declared at method level
package org.annotationmvc.vo;
import java.io.*;
import javax.persistence.*;
@Entity(access = AccessType.PROPERTY)
@Table (name="myobject")
public class MyObjectVO implements Serializable {
private int id,
private String name;
private String address;
private String email;
private String phone;
@Id (generate = GeneratorType.AUTO)
public int getId() {
return id;
}
@Column (length=100)
public String getName() {
return name;
}
@Column (length=100)
public String getAddress() {
return address;
}
@Column (length=30)
public String getEmail() {
return email;
}
@Column (length=15)
public String getPhone() {
return phone;
}
public void setXXX() {
....
}
}
Entity bean with annotations declared at variable/properties level
package org.annotationmvc.vo;
import java.io.*;
import javax.persistence.*;
@Entity(access = AccessType.FIELD)
@Table (name="myobject")
public class MyObjectVO implements Serializable {
@Id (generate = GeneratorType.AUTO)
private int id;
@Column (length=100)
private String name;
@Column (length=100)
private String address;
@Column (length=30)
private String email;
@Column (length=15)
private String phone;
public String getXXX()
{
....
}
public void setXXX() {
....
}
For this example, I have chosen to use the method-level annotations declaration.
As you can see from the previous example, every bound persistent POJO class is an entity bean and is declared by using the @Entity annotation. @Entity declares the class as an entity bean (in other words, a persistent POJO class). @Table declares the database table to which the class corresponds. This is optional. If you do not include this attribute, the default class name is used as the table name. @Id declares the identifier property of this entity bean. The other mapping declarations are implicit. The @Entity annotation also allows you to definewhether an entity bean should be accessed through its getters/setters methods or whether the entity manager should access the fields of the object directly.
Some Rules of Thumb for Defining an EntityIn short, here are the few actions you must not forget when defining an entity POJO:
* Import the javax.persistence.* packages to enable the JSDK5 annotation feature.
* At the class level, declare the @Entity and @Table to map an entity object with a table name.
* Define the access type for the POJO properties. access = AccessType.PROPERTY means that method level annotations are used, which will only be applied with the getter methods. If access = AccessType.FIELD were used, then the annotations would be associated with the fields. If AccessType is not mentioned, PROPERTY is used as the default type (in other words, for getter methods).
* For a primary key field, use the annotation @Id. There are five types of GeneratorType variables: AUTO, TABLE, IDENTITY, SEQUENCE, and NONE. Because this is a numeric type variable, you can also use SEQUENCE in this case, although the AUTO generator is the recommended type for portable applications.
* For the other properties in the POJO (in other words, not the primary field), the @Column annotation is used. Here is the syntax:
@Column(name = "address", nullable =
false, length=100, unique=false)
o name: (optional). This property refers to the corresponding table field name. If not mentioned, the default getter property is used.
o nullable: (optional). Whether null values are allowed or not for this field. Default is true.
o length: This determines the length of the field. Although optional, it is recommended to mention the field size.
o unique: (optional). This determines whether the property is unique or not. Default is false.
Create a Hibernate configuration file
The next step after defining the entity is to create a Hibernate configuration file. Start by creating a file named hibernate.cfg.xml and place it under the WEB-INF/ directory. The fully qualified class name of the POJO should be included within the tag (highlighted portion below). If more POJOs are created, they will simply be included with additional tags under the tag. (Click here to get the hibernate.cfg.xml.file.) The file should look like this
Hibernate configuration file:
hibernat.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
The DAO interface
package org.annotationmvc.dao;
public interface MyObjectDao {
MyObjectVO findMyObjectById(int id);
void insertMyObjectVO(MyObjectVO myObjectVO);
}
The DAO implementation
package org.annotationmvc.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MyObjectDaoImpl extends HibernateDaoSupport
implements MyObjectDao {
public MyObjectVO findMyObjectById(int id) {
List list=getHibernateTemplate().find("from MyObjectVO
where id=?",id);
return (MyObjectVO) list.get(0);
}
}
Spring bean configuration file :
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:global
spring
spring
class="org.springframework.orm.hibernate3.
LocalSessionFactoryBean">
WEB-INF/hibernate.cfg.xml
org.hibernate.cfg.AnnotationConfiguration
org.hibernate.dialect.
Oracle9Dialect
create
another way to create hibernate session factory
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
${connection.Driver}
${connection.url}
${connection.username}
${connection.password}
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
class="org.hibernate.cfg.DefaultComponentSafeNamingStrategy" />
${hibernate.dialect}
${hibernate.cache.use_second_level_cache}
${hibernate.cache.provider_class}
${hibernate.hbm2ddl.auto}
${hibernate.show_sql}
* At the getter-methods level
* At the object's properties level
The following example shows you how you can create an entity bean named MyObjectVO:
Entity bean with annotations declared at method level
package org.annotationmvc.vo;
import java.io.*;
import javax.persistence.*;
@Entity(access = AccessType.PROPERTY)
@Table (name="myobject")
public class MyObjectVO implements Serializable {
private int id,
private String name;
private String address;
private String email;
private String phone;
@Id (generate = GeneratorType.AUTO)
public int getId() {
return id;
}
@Column (length=100)
public String getName() {
return name;
}
@Column (length=100)
public String getAddress() {
return address;
}
@Column (length=30)
public String getEmail() {
return email;
}
@Column (length=15)
public String getPhone() {
return phone;
}
public void setXXX() {
....
}
}
Entity bean with annotations declared at variable/properties level
package org.annotationmvc.vo;
import java.io.*;
import javax.persistence.*;
@Entity(access = AccessType.FIELD)
@Table (name="myobject")
public class MyObjectVO implements Serializable {
@Id (generate = GeneratorType.AUTO)
private int id;
@Column (length=100)
private String name;
@Column (length=100)
private String address;
@Column (length=30)
private String email;
@Column (length=15)
private String phone;
public String getXXX()
{
....
}
public void setXXX() {
....
}
For this example, I have chosen to use the method-level annotations declaration.
As you can see from the previous example, every bound persistent POJO class is an entity bean and is declared by using the @Entity annotation. @Entity declares the class as an entity bean (in other words, a persistent POJO class). @Table declares the database table to which the class corresponds. This is optional. If you do not include this attribute, the default class name is used as the table name. @Id declares the identifier property of this entity bean. The other mapping declarations are implicit. The @Entity annotation also allows you to definewhether an entity bean should be accessed through its getters/setters methods or whether the entity manager should access the fields of the object directly.
Some Rules of Thumb for Defining an EntityIn short, here are the few actions you must not forget when defining an entity POJO:
* Import the javax.persistence.* packages to enable the JSDK5 annotation feature.
* At the class level, declare the @Entity and @Table to map an entity object with a table name.
* Define the access type for the POJO properties. access = AccessType.PROPERTY means that method level annotations are used, which will only be applied with the getter methods. If access = AccessType.FIELD were used, then the annotations would be associated with the fields. If AccessType is not mentioned, PROPERTY is used as the default type (in other words, for getter methods).
* For a primary key field, use the annotation @Id. There are five types of GeneratorType variables: AUTO, TABLE, IDENTITY, SEQUENCE, and NONE. Because this is a numeric type variable, you can also use SEQUENCE in this case, although the AUTO generator is the recommended type for portable applications.
* For the other properties in the POJO (in other words, not the primary field), the @Column annotation is used. Here is the syntax:
@Column(name = "address", nullable =
false, length=100, unique=false)
o name: (optional). This property refers to the corresponding table field name. If not mentioned, the default getter property is used.
o nullable: (optional). Whether null values are allowed or not for this field. Default is true.
o length: This determines the length of the field. Although optional, it is recommended to mention the field size.
o unique: (optional). This determines whether the property is unique or not. Default is false.
Create a Hibernate configuration file
The next step after defining the entity is to create a Hibernate configuration file. Start by creating a file named hibernate.cfg.xml and place it under the WEB-INF/ directory. The fully qualified class name of the POJO should be included within the
Hibernate configuration file:
hibernat.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
The DAO interface
package org.annotationmvc.dao;
public interface MyObjectDao {
MyObjectVO findMyObjectById(int id);
void insertMyObjectVO(MyObjectVO myObjectVO);
}
The DAO implementation
package org.annotationmvc.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MyObjectDaoImpl extends HibernateDaoSupport
implements MyObjectDao {
public MyObjectVO findMyObjectById(int id) {
List list=getHibernateTemplate().find("from MyObjectVO
where id=?",id);
return (MyObjectVO) list.get(0);
}
}
Spring bean configuration file :
LocalSessionFactoryBean">
Oracle9Dialect
another way to create hibernate session factory
${hibernate.dialect}
${hibernate.cache.use_second_level_cache}
${hibernate.cache.provider_class}
${hibernate.hbm2ddl.auto}
${hibernate.show_sql}
No comments:
Post a Comment