/**
*
*/
package com.hibernate.venkat;
import javax.persistence.Entity;
/**
* @author Venkat
*
*/
@Entity
public class Module extends Project {
private String ModuleName;
public String getModuleName() {
return ModuleName;
}
public void setModuleName(String moduleName) {
ModuleName = moduleName;
}
}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* @author Venkat
*
*/
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Project {
private int projectId;
private String projectName;
@Id
@GeneratedValue
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
}
---------------------------------------------------------------------------------------------
/**
*
*/
package com.hibernate.venkat;
import javax.persistence.Entity;
/**
* @author Venkat
*
*/
@Entity
public class Task extends Module{
private String taskName;
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
}
---------------------------------------------------------------------------------------------
/**
*
*/
package com.hibernate.venkat;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* @author Venkat
*
*/
public class TestInheritance {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Project.class);
config.addAnnotatedClass(Module.class);
config.addAnnotatedClass(Task.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).create(true, true);
SessionFactory factory=config.buildSessionFactory();
Session session=factory.getCurrentSession();
session.beginTransaction();
Project p=new Project();
p.setProjectName("Hibernate Project");
Module m=new Module();
m.setProjectName("Spring AOP");
m.setModuleName("AOP");
Task t=new Task();
t.setProjectName("JAVA");
t.setModuleName("Collections");
t.setTaskName("ArrayList");
session.save(p);
session.save(m);
session.save(t);
session.getTransaction().commit();
t=null;
session=factory.openSession();
session.beginTransaction();
t=(Task)session.get(Task.class, 3);
System.out.println("Task Object "+t.getProjectName());
}}
------------------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">venkat</property>
<property name="connection.password">venkat</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</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> -->
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!-- <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment