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

contact.java
------------

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;
}

}

--------------------------------------------------------
FirstExample.java
------------------
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
Configuration().configure()
.buildSessionFactory
();
session = sessionFactory.openSession
();
Transaction tx =
session.beginTransaction();
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("sita");
contact.setLastName("ram");
contact.setEmail
("sitam75@gmail.com");
session.save(contact);
tx.commit();
} catch (Exception e) {
System.out.println(e.getMessage());

} finally {
// Actual contact insertion will
happen at this step
session.flush();
session.close();

}

}
}

--------------------------------------------------------
contact.hbm.xml
---------------

<?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>
<class name="com.tutorial.hibernate.Contact"
table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

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

</hibernate-mapping>

-----------------------------------------------------
contact.hbm.xml file is a selfmapping file.

Is This Answer Correct ?    7 Yes 3 No

Post New Answer

More Advanced Java Interview Questions

difference. between List and Set?

2 Answers   Campus Interaction, HCL, Infotech,


What happens when a thread cannot acquire a lock on an object?

0 Answers  


What is the RMI and Socket?

0 Answers  


What is the form of storage space in java?

0 Answers  


how to search the pertical objects in a Collections

3 Answers   iGate,






Why use a datasource when you can directly specify a connection details?

0 Answers  


What is the difference between a stub and a skeleton?

9 Answers   ABC, College School Exams Tests, Geoservices, Polaris, Qualcomm, Rajiv Gandhi College of Engineering and Technology RGCET, UTIITSL, Wipro,


Difference between new operator and class.forname().newinstance()?

0 Answers  


what is stringBuffer and StringBuilder?

3 Answers  


Name the class that is used to bind the server object with RMI Registry?

0 Answers  


Explain Object Serialization and it can be used?

3 Answers   Infosys,


why static class in java

0 Answers   Infotech, SunGard,


Categories