-->
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.  [ 1 post ] 
Author Message
 Post subject: Using named queries from an external file
PostPosted: Wed Jun 05, 2013 3:44 pm 
Newbie

Joined: Wed Jun 05, 2013 3:24 pm
Posts: 1
Hi,

I'm trying to use named queries from a external file. The following code shows the method I want to implement

Code:
package com.lan.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lan.domain.Route;


@Component
public class RouteDAO {
   @Autowired
   private EntityManagerFactory entityManagerFactoryRW;

   public EntityManagerFactory getEntityManagerFactoryRW() {
      return entityManagerFactoryRW;
   }

   public void setEntityManagerFactoryRW(
         EntityManagerFactory entityManagerFactoryRW) {
      this.entityManagerFactoryRW = entityManagerFactoryRW;
   }

   public List<Route> findRouteByCityCode(String cityId) {
      EntityManager entityManagerRW = entityManagerFactoryRW.createEntityManager();
      TypedQuery<Route> typedQuery =  entityManagerRW.createNamedQuery("routesByCityId", Route.class);
      typedQuery.setParameter(1, cityId);
      List<Route> routeList = typedQuery.getResultList();
      return routeList;
   }
}


The query is in src/main/java/META-INF/Route.hbm.xml and it has the following content

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<sql-query name="routesByCityId">
        <return alias="route" class="com.lan.domain.Route" />
<![CDATA[SELECT * FROM booking_engine.ruta WHERE origen = ?1]]>
</sql-query>       
</hibernate-mapping>


the persistence.xml file which is in src/main/java/META-INF has the following code

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
   <persistence-unit name="mysql">

      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <mapping-file>META-INF/Route.hbm.xml</mapping-file>
      <class>com.lan.domain.Airline</class>
      <class>com.lan.domain.Route</class>

      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.format_sql" value="true" />
         <property name="hibernate.use_sql_comments" value="true" />
      </properties>
   </persistence-unit>
</persistence>


When I run the application I get the following error

Code:
11:41:22,194 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC00001: Failed to start service jboss.persistenceunit."template.rest.application.war#mysql": org.jboss.msc.service.StartException in service jboss.persistenceunit."template.rest.application.war#mysql": Failed to start service
   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [rt.jar:1.6.0_45]
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [rt.jar:1.6.0_45]
   at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mysql] Unable to build EntityManagerFactory
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
   at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
   at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
   at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
   at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
   ... 3 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate query mapping routesByCityId
   at org.hibernate.cfg.Configuration$MappingsImpl.checkQueryName(Configuration.java:2670)
   at org.hibernate.cfg.Configuration$MappingsImpl.applySQLQuery(Configuration.java:2690)
   at org.hibernate.cfg.Configuration$MappingsImpl.addSQLQuery(Configuration.java:2685)
   at org.hibernate.cfg.NamedSQLQuerySecondPass.doSecondPass(NamedSQLQuerySecondPass.java:127)
   at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1595)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1359)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
   at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
   ... 9 more


Since I'm not using the SessionFactory class and the Route entity is using annotations I'm not sure if I should have an hibernate.cfg.xml config file.

Do you know what can be causing this error ?

Thanks in advance!


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

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.