<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">hr</property> <property name="connection.password">hr</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- Names the annotated entity class --> <mapping class="com.ram.dao.Address"/> <mapping class="com.ram.dao.UserDetails"/> </session-factory> </hibernate-configuration>
In order to add this class into another class we need to mark the class as @Embeddable.
package com.ram.dao; import javax.persistence.Embeddable; @Embeddable public class Address { private String street; private String city; private String state; 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; } }
In this class we are using Address class so we need to mark the Address variable as @Embedded.
package com.ram.dao; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class UserDetails { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int userId; private String userName; @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; } public void setUserName(String userName) { this.userName = userName; } }
package com.ram.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.ram.dao.Address; import com.ram.dao.UserDetails; public class EmbeddedObjectExample { public static void main(String[] args) { UserDetails userDetails = new UserDetails(); userDetails.setUserName("Ram"); Address address = new Address(); address.setStreet("Richmond"); address.setCity("Melbourne"); address.setState("Victoria"); userDetails.setAddress(address); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(userDetails); session.getTransaction().commit(); session.close(); } }
No comments:
Post a Comment