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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <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="hibernate.hbm2ddl.auto">create</property>
  <mapping class="org.gp.gpr.dto.UserDetails"/>  
</session-factory>
</hibernate-configuration>


UserDetails.java code

package org.gp.gpr.dto;

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;

@Entity

@Table(name="USER_DETAILS")
public class UserDetails {
@Id
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;
}
@Transient
private String userName;
@Temporal(TemporalType.DATE)
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;
@Lob
private String Description;
}




HibernateTEst.java code looks like below

package org.gp.gpr.hibernate;

import java.util.Date;

import org.gp.gpr.dto.UserDetails;
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.setUserId(9);
      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();
      session.beginTransaction();
      session.save(user);
      session.getTransaction().commit();
      session.close();
      user=null;
      session=sessionFactory.openSession();
      session.beginTransaction();
      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
HibDB.jpg
HibDB2.jpg
ASKER CERTIFIED SOLUTION
Avatar of Sharon Seth
Sharon Seth
Flag of India image

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