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

What state a thread enters, When it blocks on I/O?

2 Answers  


How to implement dphibernate to activate lazy loading in Flex with java ?thanx in advance!

0 Answers  


Hello, I'm java developer. My skill is programming with java,jsp, struts, struts2+spring, hibernate, eclipse, tomcat, mysql,Oracle,Middlegen and other open source technology. I'm interest work any web base project that use my ability above. The thing is that I have never got any chance to work in this field though i am very much comfort with my skills. So if any body want me then i will happily join you. Also I have also some of my friends if you. If you interest to outsource any project to me then also you are welcome. you can reach me by email at me4bangalore@yahoo.in

4 Answers  


which deployment descriptor element is used to configure the authentication method? a. auth-config b. login-config c. sec-config

1 Answers  


What is Stream and Types?

2 Answers   Patni,






What exceptions are thrown by RMI?

1 Answers  


Which component handles cluster communication in jboss?

0 Answers  


What if the static modifier is removed from the signature of the main method?

0 Answers  


What is threadfactory?

0 Answers  


What is the difference between a static and a non-static inner class?

0 Answers  


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

0 Answers  


Difference between JRE and JVM?

3 Answers   HeadStrong, Infotech,


Categories