Home  >  Hibernate

Generating Annotated POJOs in Hibernate

By Arvind Rai, May 12, 2013
In hibernate annotation POJO can be created annotation based and then no need of hbm file. Annotated POJO is easy to maintain and to understand.

User.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")  
public class User {
	@Id
	@GeneratedValue
	private int id;
		
	@Column(name="name")
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
 
@Entity : denotes that this is a POJO to be persistent.

@Table(name="user") : Maps the class with table

@Id: makes this column primary

@GeneratedValue: makes the column to be auto generated

@Column(name="name"): maps the class variable with column name of the table.

Configure POJO in Hibernate.cfg.xml

Annotated POJO is also configured in hibernate.cfg.xml. It is done as

 <mapping class="com.concretepage.persistence.User"/>
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us