gudii9
asked on
hibernate value object
I was going through hibernate value object related example.
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
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_cl ass">org.p ostgresql. Driver</pr operty>
<property name="hibernate.connection.url">jdbc :postgresq l://localh ost:5432/h ibernatedb </property >
<property name="hibernate.connection.username" >postgres< /property>
<property name="hibernate.connection.password" >root</pro perty>
<property name="hibersnate.connection.pool_siz e">10</pro perty>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect .PostgreSQ LDialect</ 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().buildSes sionFactor y();
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
are both tags
@Embeddable and
@Embedded
are mandotary or just optional?
please advise
@Embeddable and
@Embedded
are mandotary or just optional?
please advise
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.