Dude, can you explain better? I simulated your code here, and it works as expected:
I tested with Hibernate 3.2.3ga, which is the same you used. I could not identity whether you used pure JTA or Hibernate Annotations + Hibernate Entity Manager + Hibernate Core. Which version of Hibernate Entity Manager are you using?
In my test, I used only JTA, being Hibernate the implementation of it.
This is my test class, using JUnit 4.x:
Code:
package polymorphism;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
public class Runner {
private EntityManager em;
private Logger logger = Logger.getLogger(Runner.class);
@Test
public void query() {
createData();
String queryString = "from CashPayment as cp where state = 1";
final Query query = em.createQuery(queryString);
final List<Payment> result = query.getResultList();
assertEquals(result.size(), 1);
logger.debug("How many results?" + result.size());
}
private void createData() {
em.getTransaction().begin();
CashPayment cash1 = new CashPayment();
cash1.setName("Money");
cash1.setState(true);
em.persist(cash1);
CreditCardPayment cc1 = new CreditCardPayment();
cc1.setName("Visa");
cc1.setState(true);
em.persist(cc1);
em.getTransaction().commit();
}
@Before
public void setup() {
logger.debug("Setting up session factory");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernate-persistence");
em = emf.createEntityManager();
}
}
This is the debug output:
Code:
19:21:20,459 DEBUG Runner:49 - Setting up session factory
19:21:22,105 INFO SchemaExport:154 - Running hbm2ddl schema export
19:21:22,107 DEBUG SchemaExport:170 - import file not found: /import.sql
19:21:22,108 INFO SchemaExport:179 - exporting generated schema to database
19:21:22,114 DEBUG SchemaExport:303 - drop table if exists payment
19:21:22,161 DEBUG SchemaExport:303 - create table payment (payment_class varchar(31) not null, id bigint not null auto_increment, name varchar(255), state bit not null, primary key (id))
19:21:22,169 INFO SchemaExport:196 - schema export complete
19:21:22,240 DEBUG SQL:424 - insert into payment (name, state, payment_class) values (?, ?, 'CashPayment')
19:21:22,271 DEBUG SQL:424 - insert into payment (name, state, payment_class) values (?, ?, 'CreditCardPayment')
19:21:22,453 DEBUG SQL:424 - select cashpaymen0_.id as id0_, cashpaymen0_.name as name0_, cashpaymen0_.state as state0_ from payment cashpaymen0_ where cashpaymen0_.payment_class='CashPayment' and cashpaymen0_.state=1
19:21:22,459 DEBUG Runner:28 - How many results?1
This is my persistence.xml, located in META-INF:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="hibernate-persistence"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>polymorphism.Payment</class>
<class>polymorphism.CashPayment</class>
<class>polymorphism.ChequePayment</class>
<class>polymorphism.CreditCardPayment</class>
<properties>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/hibernate" />
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password"
value="hibernate" />
<property name="hibernate.connection.username"
value="hibernate" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
These are the annotations in the abstract Payment class:
Code:
@Entity
@Table(name = "payment")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Proxy(lazy = false)
@DiscriminatorColumn(name = "payment_class", discriminatorType = DiscriminatorType.STRING)
And these are the annotations in the concrete classes, changing only the DiscriminatorValue:
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("CreditCardPayment")