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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Why use a datasource when you can directly specify a connection details? (in a J2EE application)

3461


If I wanted to use a solarisui for just a jtabbedpane, and the metal ui for everything else, how would I do that?

572


What must a class do to implement an interface?

586


What is an abstract method?

596


Name the eight primitive java types.

601






How to implement RMI in Java?

2408


Define prototype?

585


How database connectivity in XML is achieved?

1769


What classes of exceptions may be caught by a catch clause?

557


Why doesn’t the focus feature on the tag work in every circumstance?

573


AS a developer will u create a data source in connection pool? If so how will u do that, how to access the object from connection pool using IRAD tool?

1642


what is handle?

1857


cud u help me ... i am struggling with this question... to find all the subsets of a given set for ex.... a,,b,c shud give all the subsets.... i gt the program in c bt nt able to get it in java..... help needed ..

1739


what are the activation groupworks?

1679


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

602