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

asked on

hibernate value object

I was going through hibernate value object related 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 looks like

package org.gp.gpr.dto;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
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 @GeneratedValue
private int userId;
@Embedded
private Address address;

public Address getAddress() {
      return address;
}
public void setAddress(Address address) {
      this.address = address;
}
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 getDescription() {
      return Description;
}
public void setDescription(String description) {
      Description = description;
}


@Lob
private String Description;
}



Address.java code looks like

package org.gp.gpr.dto;

import javax.persistence.Embeddable;

@Embeddable
public class Address {
      private String street;
      private String city;
      private String state;
      private String pincode;
      public String getStreet() {
            return street;
      }
      public void setStreet(String street) {
            this.street = street;
      }
      public String getCity() {
            return city;
      }
      public void setCity(String city) {
            this.city = city;
      }
      public String getState() {
            return state;
      }
      public void setState(String state) {
            this.state = state;
      }
      public String getPincode() {
            return pincode;
      }
      public void setPincode(String pincode) {
            this.pincode = pincode;
      }

}


Modified Address.java looks like this

package org.gp.gpr.dto;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
      @Column(name="STREET_NAME")
      private String street;      
      @Column(name="CITY_NAME")
      private String city;
      @Column(name="STATE_NAME")
      private String state;
      @Column(name="PINCODE_NAME")
      private String pincode;
      public String getStreet() {
            return street;
      }
      public void setStreet(String street) {
            this.street = street;
      }
      public String getCity() {
            return city;
      }
      public void setCity(String city) {
            this.city = city;
      }
      public String getState() {
            return state;
      }
      public void setState(String state) {
            this.state = state;
      }
      public String getPincode() {
            return pincode;
      }
      public void setPincode(String pincode) {
            this.pincode = pincode;
      }

}




HibernateTestValuObject class looks like


package org.gp.gpr.hibernate;

import java.util.Date;

import org.gp.gpr.dto.Address;
import org.gp.gpr.dto.UserDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTestValueObject {

public static void main(String[] args){
      UserDetails user=new UserDetails();
      //user.setUserId(9);
      user.setUserName("1stth User");
      //UserDetails user2=new UserDetails();
//      user2.setUserName("2nd User");
      Address addr=new Address();
      addr.setStreet("main street");
      addr.setCity("london");
      user.setAddress(addr);
      //user2.setAddress(addr);
      /*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.save(user2);
      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());*/
      
}
}






I have question in my mind. When there is

@Embeddable tag

why we need to separately mention
@Emmbedded tag.

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
ValueObjectHib.png
ValueObjectHib2.jpg
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

@Embedded means it will embed all details of the Embeddable object in the same table.

in the USER table itself it will store the address details.

if you dont mention @Embedded tag then how hibernate knows that the details of address oject should be stored in the same users table.
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gudii9

ASKER

are both tags

 @Embeddable and
@Embedded
are mandotary or just optional?
please advise