Ver Mensaje Individual
  #6 (permalink)  
Antiguo 09/08/2011, 17:05
nicostone
 
Fecha de Ingreso: marzo-2009
Mensajes: 76
Antigüedad: 15 años, 1 mes
Puntos: 1
Respuesta: Hibernate + Herencia

CLASE PERSONA

Código:
package persona;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;


@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Persona {
	
	protected String nombre;
	
	protected String apellido;
	
	protected String direccion;
	
	protected String telefono;
	
	protected String celular;
	
	protected String email;
	
	public Persona() {
		super();
	}
	
	@Column(name = "Nombre")
	public String getNombre() {
		return nombre;
	}
	public void setNombre(String nombre) {
		this.nombre = nombre;
	}
	@Column(name = "Apelldio")
	public String getApellido() {
		return apellido;
	}
	public void setApellido(String apellido) {
		this.apellido = apellido;
	}
	@Column(name = "Direccion")
	public String getDireccion() {
		return direccion;
	}
	public void setDireccion(String direccion) {
		this.direccion = direccion;
	}
	@Column(name = "Telefono")
	public String getTelefono() {
		return telefono;
	}
	public void setTelefono(String telefono) {
		this.telefono = telefono;
	}
	@Column(name = "Celular")
	public String getCelular() {
		return celular;
	}
	public void setCelular(String celular) {
		this.celular = celular;
	}
	@Column(name = "Email")
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}
CLASE EMPLEADO CONTRATADO
Código:
package empleado;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import persona.Persona;




@Entity
@Table(name = "EmpleadosContratados")
public class EmpleadoContratado extends Persona{

	@GeneratedValue
	@Id
	@Column(name = "Id_Empleado")
	private long idEmpleado;
	@Column(name ="Especialidad")
	private String especialidad;

	public EmpleadoContatado () {
		super();
	}

	public String getEspecialidad() {
		return especialidad;
	}

	
public void setEspecialidad(String especialidad) {
		this.especialidad = especialidad;
	}


	public long getIdEmpleado() {
		return idEmpleado;
	}

	public void setIdEmpleado (long idEmpleado) {
		this.idEmpleado = idEmpleado;
	}
}
El Archivo test-applicationContext.xml (Igual No creo que este archivo sea el problema ya que al sacar el @Entity no tirar error, no funciona como yo quisiera pero no tira error)

Código:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<context:component-scan base-package="persona" annotation-config="true"/>
	<context:component-scan base-package="empleado" annotation-config="true"/>
	
	<context:component-scan base-package="testServicios" annotation-config="true"/>	

<!-- **************** CONFIGURACION DE HIBERNATE ************** -->

	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/NombreBD " />
		<property name="username" value="root" />
		<property name="password" value="claveRoot" /> 
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="myDataSource"/>
		<property name="annotatedClasses">
			<list>
 				<value>persona.Persona</value>
				<value>empleado.EmpleadoContratado </value>
			</list>

		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop> 
			</props>
		</property>
	</bean>

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
		abstract="false" lazy-init="default" autowire="default"
		dependency-check="default">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>

	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<!-- the transactional semantics... -->
		<tx:attributes>
			<!-- all methods starting with 'get' are read-only -->
			<tx:method name="get*" read-only="true" />
			<!-- other methods use the default transaction settings (see below) -->
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
</beans>
Espero que el codigo ayude, gracias otra vez y saludos.

NicoStone