Java class are mapped to columns of a DB table in Hibernate

There are two way we can mapped to columns of a DB table to the properties of a Java class in Hibernate.
Using the XML file :-
   We can map the column of a table to the property of a class in XML file.it will be describe inside hbm.xml file.
    There we are declare property name, class name and type of variable.

				
					<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.Employee" table="Employee">
		<id name="id" type="int" column="id">
			<generator class="native" />
		</id>
		<property name="firstname" column="firstname" type="string" />
		<property name="lastname" column="lastname" type="string" />
		<property name="salary" column="salary" type="int" />
	</class>
</hibernate-mapping>
				
			

Using annotations :-

  • There we are use annotations @Entity, @Table and @Column to map a column to the property of a class.
  • we are use @Column to explicitly define the column name that is mapped to the a property.
  • If no @Column is used then it will use the configured naming strategies to determine which column should a property mapped to.
				
					import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

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

@Column(name="firstName")
private String firstname;

@Column(name="lastName")
private String lastname;

@Column(name="Salary")
private int salary;