Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

hibernate example

i was running  below hibernate example

hibernate.cfg.xml code

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

  <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
  <property name="hibernate.connection.username">postgres</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibersnate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="">create</property>
  <mapping class=""/>  
</hibernate-configuration> code


import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

//import org.hibernate.annotations.Entity;


public class UserDetails {
private int userId;

public int getUserId() {
      return userId;
public void setUserId(int userId) {
      this.userId = userId;

public String getUserName() {
      return userName+"from get";
public void setUserName(String userName) {
      this.userName = userName;
private String userName;
private Date joinedDate;
public Date getJoinedDate() {
      return joinedDate;
public void setJoinedDate(Date joinedDate) {
      this.joinedDate = joinedDate;
public String getAddress() {
      return Address;
public void setAddress(String address) {
      Address = address;
public String getDescription() {
      return Description;
public void setDescription(String description) {
      Description = description;

private String Address;
private String Description;
} code looks like below


import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {

public static void main(String[] args){
      UserDetails user=new UserDetails();
      user.setUserName("9th User");
      user.setJoinedDate(new Date());
      user.setAddress("9th user addr");
      user.setDescription("9th user desc");
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();
      user=(UserDetails) session.get(UserDetails.class, 9);
      System.out.println("user description retrrieded is"+user.getDescription());

My question is while reading the user object description Field i used below 2 lines of  code

user=(UserDetails) session.get(UserDetails.class, 9);
System.out.println("user description retrrieded is"+user.getDescription());

I wonder how hibernate understood 9 as primary key column id of the data. Please advise.

 Any ideas, resources, sample code highly appreciated. thanks in advance
Avatar of Sharon Seth
Sharon Seth
Flag of India image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account