Follow Our FB Page << >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

Dear freinds... I want to know how to write self mapping
for a table using hibernate?

Dear freinds... I want to know how to write self mapping for a table using hibernate?..

Answer / dsr

package com.tutorial.hibernate;

public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

public String getEmail() {
return email;

public String getFirstName() {
return firstName;

public String getLastName() {
return lastName;

public void setEmail(String string) {
email = string;

public void setFirstName(String string) {
firstName = string;

public void setLastName(String string) {
lastName = string;

public long getId() {
return id;

public void setId(long l) {
id = l;


package com.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new
session = sessionFactory.openSession
Transaction tx =
Contact contact = new Contact();
} catch (Exception e) {

} finally {
// Actual contact insertion will
happen at this step




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.tutorial.hibernate.Contact"
<id name="id" type="long" column="ID" >
<generator class="assigned"/>

<property name="firstName">
<column name="FIRSTNAME" />
<property name="lastName">
<column name="LASTNAME"/>
<property name="email">
<column name="EMAIL"/>


contact.hbm.xml file is a selfmapping file.

Is This Answer Correct ?    7 Yes 3 No

Post New Answer

More Advanced Java Interview Questions

when we applied start()method on a thread ,how does it know that to execute run()method on that object?

2 Answers   HCL,

what is the Remote Stub?

1 Answers  

what is the Scope of Final Keyword in Java?

1 Answers   HCL,

what is meant by JRMP?

0 Answers  

What are different types of controls in AWT?

11 Answers   IBM,

How are commas used in the intialization and iteration parts of a for statement?

0 Answers  

What is mdb and what is the special feature of that?

0 Answers  

How to pass parameters in RMI?

0 Answers  

How are the elements of a borderlayout organized?

0 Answers  

What is private static final long serialVersionUID = 1L;

3 Answers   Google,

what is difference between checked exception & unchecked exception in java?

3 Answers   Ness Technologies,

What restrictions are placed on the location of a package statement within a source code file?

0 Answers