-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Polymorphism query returns to many results
PostPosted: Thu Aug 07, 2008 9:50 am 
Newbie

Joined: Wed Jan 16, 2008 2:22 am
Posts: 4
Hibernate version: 3.2.3.ga

Name and version of the database you are using: SQL Server 2005

Code:
@Entity
@Table(name="PAYMENT")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Proxy(lazy=false)
@DiscriminatorColumn(name="PAYMENT_CLASS", discriminatorType=DiscriminatorType.STRING)
abstract class Payment

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("CreditCardPayment")
class CreditCardPayment

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("ChequePayment")
class ChequePayment

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("CashPayment")
class CashPayment



In my dao i have the following code :

Code:
String queryString = "from CashPayment as cp where state = 1"
final Query query = session.createQuery(queryString);
final List result = query.list();


the result list does not contain the correct data. In my database there are 2 records that would be found via that query, but hibernate returns 6 Payment objects. 2 for each subclass.

Is there something wrong with the mapping ? incorrect InheritanceType ? ... ?

Thanks in advance


Top
 Profile  
 
 Post subject: Can you explain better?
PostPosted: Thu Aug 07, 2008 1:25 pm 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
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")


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.