如何在JPA 2.0项目中加载Hibernate的'xxx.hbm.cfg'文件? [英] How to load a Hibernate 'xxx.hbm.cfg' file in a JPA 2.0 project?

查看:88
本文介绍了如何在JPA 2.0项目中加载Hibernate的'xxx.hbm.cfg'文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Hibernate作为JPA2.0提供者的Spring Roo应用程序。我使用的jar如下:

hibernate-core-3.6.4.Final.jar



hibernate-commons-annotations-3.2.0.jar
$ b hibernate-entitymanager-3.6.4。 Final.jar
$ b

hibernate-jpa-2.0-api-1.0.0.Final.jar



hibernate-validator-4.1.0.Final.jar



我使用Annotations来处理事务应用程序的方面,没有问题。 但是还有其他应用程序部分需要非常复杂的查询,而我以前在Hibernate中处理的方式是创建一个映射文件,例如(mybigdwquery.hbm .xml)在那里我会指定我的查询和它的映射对象,一个POJO。不是@实体。然而,通过我之前发布的另一个问题,我发现在JPA 2.0中,不能将查询映射到POJO,因此必须映射所有内容到一个@Entity(一个数据库表没有?)。

所以我的问题如下:



是否有任何方法可以让我的'mybigdwquery.hbm.xml'文件作为hbm.xml加载到persistence.xml中,以便可以调用指定的查询?



我的persistence.xml如下:

 < xml version =1.0encoding =UTF-8standalone =no?> 
< persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instanceversion = 2.0xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\">
< persistence-unit name =persistenceUnittransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<属性>
<! - < property name =hibernate.dialectvalue =org.hibernate.dialect.HSQLDialect/> - >
<! - value =create在每次运行中构建新的数据库; value =update来修改现有的数据库; value =create-drop与create相同,但在Hibernate关闭时也会丢弃表; value =validate不会更改数据库 - >
< property name =hibernate.dialectvalue =org.hibernate.dialect.Oracle10gDialect/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.ejb.naming_strategyvalue =org.hibernate.cfg.ImprovedNamingStrategy/>
< property name =hibernate.connection.charSetvalue =UTF-8/>
< / properties>
< / persistence-unit>
< /余辉>

我需要加载的文件:

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0 .dtd>
< hibernate-mapping package =edu.kdc.visioncards.pojo>

< class name =AttendanceBreakDown>
< cache usage =read-only/>
< id name =studentName/>
< property name =pupilId>< / property>
< property name =enrollmentStatus>< / property>
< property name =attendanceLevel>< / property>
< property name =attendanceDays>< / property>
< property name =authorizedAbsences>< / property>
< property name =unexcusedAbsences>< / property>
< property name =excusedAbsences>< / property>
< property name =tardies>< / property>
< property name =attendancePct>< / property>
< / class>

< sql-query name =attendanceDetailsBySchoolAndGradingPeriod>
< return alias =attSchGrclass =edu.kdc.visioncards.pojo.AttendanceBreakDown>
< return-property name =studentNamecolumn =student_name/>
< return-property name =pupilIdcolumn =student_id/>
< return-property name =enrollmentStatuscolumn =enrollment_status/>
< return-property name =attendanceLevelcolumn =attendance_Level/>
< return-property name =attendanceDayscolumn =attendance_days/>
< return-property name =authorizedAbsencescolumn =auth_abs/>
< return-property name =unexcusedAbsencescolumn =unx_abs/>
< return-property name =excusedAbsencescolumn =x_abs/>
< return-property name =tardiescolumn =tardies/>
< return-property name =attendancePctcolumn =att_pct/>
< / return>
选择
a.student_name
,a.student_id
,a.enrollment_status
,a.attendance_days
,a.Attendance_Level
,b .authorized_absences as auth_abs
,nvl(c.unx_abs,0)as unx_abs
,nvl(d.x_abs,0)as x_abs
,nvl(e.tardies,0)as tardies
,a.att_pct
from
(选择
s.student_name
,s.student_id
,s.student_activity_indicator as enrollment_status
,sum( ((sum(fas.attendance_days))* 100,2)作为att_pct
,考勤值)/总和(fas.attendance_days))* 100,2)& lt; = 87)
然后'Intervene'
((sum(fas.attendance_value)/ sum(fas.attendance_days))* 100,2)> 87和
round((sum(fas.attendance_value)/ sum(fas.attendance_days))* ((sum(fas.attendance_value)/ sum(fas.attendance_days))* 100,2)> => = 89.9)
然后'关注'
((sum(fas.attendance_value)/ sum(fas.attendance_days))* 100,2)& lt; = 95)
然后'Baseline'
else'Is Clean'
end AS Attendance_Level
from K12INTEL_DW.ftbl_attendance_stumonabssum fas
内部连接k12intel_dw.dtbl_students s
on fas.student_key = s.student_key
内部连接K12INTEL_DW.dtbl_schools ds
fas.school_key = ds.school_key
内部连接k12intel_dw.dtbl_school_dates dsd
在fas.school_dates_key = dsd.school_dates_key
其中dsd.rolling_local_school_yr_number = 0
和ds.school_code =?
和s.student_activity_indicator ='有效'
和fas.LOCAL_GRADING_PERIOD =?
和s.student_current_grade_level =?
group by s.student_id,s.student_name,s.student_activity_indicator
having(sum(fas.attendance_value)/ sum(fas.attendance_days))& lt; .95
)a
内部连接
(选择t.student_id
,sum(t.auth_abs)为authorized_absences
from(
select dstud.student_id
,在('NA','否')
然后0 else 1
(fas.excused_authorized)结束时作为auth_abs
from K12INTEL_DW.ftbl_attendance_stumonabssum fas
内部加入K12INTEL_DW.dtbl_schools ds
在fas.school_key = ds.school_key
内部加入k12intel_dw.dtbl_students dstud
在dstud.student_key = fas.student_key
内部加入k12intel_dw.dtbl_school_dates dsd
dsd.school_dates_key = fas.school_dates_key
其中dsd.rolling_local_school_yr_number = 0
和dstud.student_activity_indicator ='有效'
和ds.school_code =?
和fas.LOCAL_GRADING_PERIOD =?
和dstud.student_current_grade_level =?
)t
由t.student_id分组)b
对b.student_id = a.student_id
离开外部连接
(选择dstud.student_id,
count(fas.excused_absence)as unx_abs
from K12INTEL_DW.ftbl_attendance_stumonabssum fas
inner join K12INTEL_DW.dtbl_schools ds
on fas.school_key = ds.school_key
inner join k12intel_dw.dtbl_students dstud
on dstud.student_key = fas.student_key
内部连接k12intel_dw.dtbl_school_dates dsd
dsd.school_dates_key = fas.school_dates_key
其中dsd.rolling_local_school_yr_number = 0
和dstud。 student_activity_indicator ='有效'
和fas.excused_absence ='不合理的缺席'
和ds.school_code =?
和fas.LOCAL_GRADING_PERIOD =?
和dstud.student_current_grade_level =?
group by dstud.student_id
)c
on c.student_id = a.student_id
left outer join
(select dstud.student_id,count(fas.excused_absence) as x_abs
from K12INTEL_DW.ftbl_attendance_stumonabssum fas
inner join K12INTEL_DW.dtbl_schools ds
on fas.school_key = ds.school_key
inner join k12intel_dw.dtbl_students dstud
on dstud。 student_key = fas.student_key
inner join k12intel_dw.dtbl_school_dates dsd
dsd.school_dates_key = fas.school_dates_key
where dsd.rolling_local_school_yr_number = 0
and dstud.student_activity_indicator ='Active'
和fas.excused_absence ='无故缺席'
和ds.school_code =?
和fas.LOCAL_GRADING_PERIOD =?
和dstud.student_current_grade_level =?
group by dstud.student_id)d
on d.student_id = a.student_id
left outer join
(select s.student_id
,sum(a.attendance_value) tardies
from k12intel_dw.ftbl_attendance a
内部连接k12intel_dw.dtbl_school_dates sd
上a.school_dates_key = sd.school_dates_key
内连接k12intel_dw.dtbl_students s
上a.student_key = s.student_key
内部连接k12intel_dw.dtbl_schools sc
上sc.school_key = s.school_key
其中1 = 1
和sd.rolling_local_school_yr_number = 0
和a ('LA','LP','LF')
和sc.school_code =?
和s.student_current_grade_level =?
group by s.student_id)中的.attendance_type e
在e.student_id上= a.student_id
< / sql-query>

< / hibernate-mapping>

这是我的DAO:

  @Repository 
public class K12DaoImpl implements K12DaoManager {

@PersistenceContext
private EntityManager em;

// @Autowired
// private SessionFactory sessionFactory;
//
// public void setSessionFactory(SessionFactory sessionFactory){
// this.sessionFactory = sessionFactory;
//}

@Override
@Transactional(readOnly = true)
public List< AttendanceBreakDown> getAttendanceBreakDownBySchoolAndGP(int school,String gradingPeriod,String gradeLevel){

对象值[] = new Object [] {new Integer(school),gradingPeriod,gradeLevel,
new Integer(school), (学校),等级时间,成绩等级,
新整数(学校),等级时间,成绩等级,
新整数(学校),成绩等级
};
//通过JPA
调用命名查询// Query query = em.createNamedQuery(attendanceDetailsBySchoolAndGradingPeriod);
//
// for(int i = 0; i< values.length; i ++){
// query.setParameter(i,values [i]);
//}
//
//列表< AttendanceBreakDown> list = query.getResultList();
//

//通过Hibernate的SessionFactory调用命名查询
// org.hibernate.Query query = sessionFactory.getCurrentSession()。getNamedQuery(attendanceDetailsBySchoolAndGradingPeriod);
//
// for(int i = 0; i< values.length; i ++){
// query.setParameter(i,values [i]);
//}
//
//列表< AttendanceBreakDown> list = query.list();

//通过HibernateTemplate调用命名查询
// List< AttendanceBreakDown> list = getHibernateTemplate()。findByNamedQuery(attendanceDetailsBySchoolAndGradingPeriod,values);

//返回列表;
返回null;


在不使用persistence.xml的情况下, hibernate.cfg.xml设置里面的一个applicationContext-datasource与它会话工厂,会话工厂绑定到数据源等等。一切都很好。



现在我拥有持久性.xml,现在不再有SessionFactory,EntityManager。



如何加载hbm.xml文件并通过Hibernate而不是通过JPA 2.0执行它们?



如果你会看到DAO中的注释代码如果我正在使用Hibernate配置来调用通过HibernateTemplate(扩展HibernateDaoSupport)的命名查询正在工作。现在的代码会是什么样子?



谢谢

解决方案

I已经找到了我自己的问题的答案。为了使它工作,我做了这些:


  1. 使用< mapping-file>。 ..hbm.xml< / mapping-file> < persistence-unit> 中的 persistence.xml 中。我真的没有使用< mapping-file> ... hbm.xml< / mapping-file> 标记,因为这给我带来了各种各样的异常,其中一个是DuplicateMappingException。根据文档,我也认为我必须使用该标签,但事实证明,您不必这样做。

  2. 创建 edu / kdc src / main / resources 下的/visioncards/pojo/AttendanceBreakDown.hbm.xml
  3. 最后,在我的 DAO ,我有如下:

      @Override 
    @Transactional(readOnly = true)
    public List< AttendanceBreakDown> getAttendanceBreakDownBySchoolAndGP(int school,String graddingPeriod,String gradeLevel){
    Object values [] = new Object [] {new Integer(school),gradingPeriod,gradeLevel,
    new Integer(school),gradingPeriod,gradeLevel,
    新整数(学校),等级时间,等级,
    新整数(学校),评分时间,等级,
    新整数(学校),等级
    ;

    org.hibernate.Session session =(Session)em.getDelegate();
    org.hibernate.Query query = session.getNamedQuery(attendanceDetailsBySchoolAndGradingPeriod);
    for(int i = 0; i< values.length; i ++){
    query.setParameter(i,values [i]);
    }
    List< AttendanceBreakDown> list = query.list();
    返回列表;
    }


现在我可以使用JPA 2.0到EntityManager并退到Hibernate的会话中,以访问JPA 2.0不提供的所有Hibernate功能。


I have just started a Spring Roo application with Hibernate as a JPA2.0 provider. I am using the jars as follows:

hibernate-core-3.6.4.Final.jar

hibernate-commons-annotations-3.2.0.jar

hibernate-entitymanager-3.6.4.Final.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate-validator-4.1.0.Final.jar

I am using Annotations to handle the transactional aspect of the application, no problem there.

But there are other parts of the application that require very complex queries, and the way I had it handled in Hibernate before was to create a mapping file e.g (mybigdwquery.hbm.xml) where I would specify my query and its mapping object, a POJO. Not an @Entity. This works fine.

However, Through another question that I previously posted, I found out that in JPA 2.0 you cannot have queries mapped to a POJO, everything has to be mapped to an @Entity (a db table no?).

So my question is as follows:

Is there any way that I can have my 'mybigdwquery.hbm.xml' file loaded in my persistence.xml as a hbm.xml so that I can call the named query?

My persistence.xml is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>             
        <properties>
            <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> -->
            <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->            
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>                                   
        </properties>
    </persistence-unit>    
</persistence>

The file that I need loaded:

<?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 package="edu.kdc.visioncards.pojo">

    <class name="AttendanceBreakDown">
        <cache usage="read-only" />
        <id name="studentName"/>
        <property name="pupilId"></property>
        <property name="enrollmentStatus"></property>
        <property name="attendanceLevel"></property>
        <property name="attendanceDays"></property>
        <property name="authorizedAbsences"></property>
        <property name="unexcusedAbsences"></property>
        <property name="excusedAbsences"></property>
        <property name="tardies"></property>
        <property name="attendancePct"></property>
    </class>

    <sql-query name="attendanceDetailsBySchoolAndGradingPeriod">
        <return alias="attSchGr" class="edu.kdc.visioncards.pojo.AttendanceBreakDown">
         <return-property name="studentName" column="student_name"/>
         <return-property name="pupilId" column="student_id"/>
         <return-property name="enrollmentStatus" column="enrollment_status"/>
         <return-property name="attendanceLevel" column="attendance_Level"/>
         <return-property name="attendanceDays" column="attendance_days"/>
         <return-property name="authorizedAbsences" column="auth_abs"/>
         <return-property name="unexcusedAbsences" column="unx_abs"/>
         <return-property name="excusedAbsences" column="x_abs"/>
         <return-property name="tardies" column="tardies"/>
         <return-property name="attendancePct" column="att_pct"/>
         </return>
          select
                a.student_name
               ,a.student_id
               ,a.enrollment_status
               ,a.attendance_days
               ,a.Attendance_Level
               ,b.authorized_absences as auth_abs
               ,nvl(c.unx_abs,0) as unx_abs
               ,nvl(d.x_abs, 0) as x_abs
               ,nvl(e.tardies, 0) as tardies
               ,a.att_pct
          from
              (select
                   s.student_name
                  ,s.student_id
                  ,s.student_activity_indicator as enrollment_status
                  ,sum(fas.attendance_days) as attendance_days
                  ,round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) as att_pct
                  ,case when(round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) &lt;= 87)
                                then 'Intervene'
                                when(round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) >87 and
                                     round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) &lt;= 89.9)
                                then 'Concern'
                                when(round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) >=90 and
                                     round((sum(fas.attendance_value) / sum(fas.attendance_days))* 100,2) &lt;= 95)
                                then 'Baseline'
                                else 'Is Clean'
                           end AS Attendance_Level
                  from K12INTEL_DW.ftbl_attendance_stumonabssum fas
                   inner join k12intel_dw.dtbl_students s
                       on fas.student_key = s.student_key
                  inner join K12INTEL_DW.dtbl_schools ds
                      on fas.school_key = ds.school_key
                  inner join k12intel_dw.dtbl_school_dates dsd
                      on fas.school_dates_key = dsd.school_dates_key
                  where dsd.rolling_local_school_yr_number = 0
                  and ds.school_code = ?
                  and s.student_activity_indicator = 'Active'
                  and fas.LOCAL_GRADING_PERIOD = ?
                  and s.student_current_grade_level = ?
                  group by s.student_id, s.student_name, s.student_activity_indicator
                  having (sum(fas.attendance_value) / sum(fas.attendance_days)) &lt; .95
               ) a
          inner join
                  (select t.student_id
                   ,sum(t.auth_abs) as authorized_absences
                  from(
                      select dstud.student_id
                             ,case when(fas.excused_authorized) in ('NA', 'No')
                             then 0 else 1
                             end as auth_abs
                      from K12INTEL_DW.ftbl_attendance_stumonabssum fas
                      inner join K12INTEL_DW.dtbl_schools ds
                          on fas.school_key = ds.school_key
                      inner join k12intel_dw.dtbl_students dstud
                          on dstud.student_key = fas.student_key
                      inner join k12intel_dw.dtbl_school_dates dsd
                          on dsd.school_dates_key = fas.school_dates_key
                      where dsd.rolling_local_school_yr_number = 0
                      and dstud.student_activity_indicator = 'Active'
                      and ds.school_code = ?
                      and fas.LOCAL_GRADING_PERIOD = ?
                      and dstud.student_current_grade_level = ?
                   ) t
                  group by t.student_id)b
          on b.student_id = a.student_id
          left outer join
                      ( select dstud.student_id,
                               count(fas.excused_absence) as unx_abs
                          from K12INTEL_DW.ftbl_attendance_stumonabssum fas
                          inner join K12INTEL_DW.dtbl_schools ds
                              on fas.school_key = ds.school_key
                          inner join k12intel_dw.dtbl_students dstud
                              on dstud.student_key = fas.student_key
                          inner join k12intel_dw.dtbl_school_dates dsd
                              on dsd.school_dates_key = fas.school_dates_key
                          where dsd.rolling_local_school_yr_number = 0
                          and dstud.student_activity_indicator = 'Active'
                          and fas.excused_absence = 'Un-excused absence'
                          and ds.school_code = ?
                          and fas.LOCAL_GRADING_PERIOD = ?
                          and dstud.student_current_grade_level = ?
                          group by dstud.student_id
                       ) c
          on c.student_id = a.student_id
          left outer join
              (select dstud.student_id, count(fas.excused_absence) as x_abs
                  from K12INTEL_DW.ftbl_attendance_stumonabssum fas
                  inner join K12INTEL_DW.dtbl_schools ds
                      on fas.school_key = ds.school_key
                  inner join k12intel_dw.dtbl_students dstud
                      on dstud.student_key = fas.student_key
                  inner join k12intel_dw.dtbl_school_dates dsd
                      on dsd.school_dates_key = fas.school_dates_key
                  where dsd.rolling_local_school_yr_number = 0
                  and dstud.student_activity_indicator = 'Active'
                  and fas.excused_absence = 'Excused absence'
                  and ds.school_code = ?
                  and fas.LOCAL_GRADING_PERIOD = ?
                  and dstud.student_current_grade_level = ?
                  group by dstud.student_id) d
          on d.student_id = a.student_id
          left outer join
              (select s.student_id
                     ,sum(a.attendance_value) tardies
                from k12intel_dw.ftbl_attendance a
                inner join k12intel_dw.dtbl_school_dates sd
                    on a.school_dates_key = sd.school_dates_key
                inner join k12intel_dw.dtbl_students s
                  on a.student_key = s.student_key
                inner join k12intel_dw.dtbl_schools  sc
                    on sc.school_key = s.school_key
                where 1=1
                and sd.rolling_local_school_yr_number = 0
                and a.attendance_type in ('LA','LP','LF')
                and sc.school_code= ?
                and s.student_current_grade_level = ?
                group by s.student_id) e
          on e.student_id = a.student_id
    </sql-query>   

</hibernate-mapping>

This is my DAO:

@Repository
public class K12DaoImpl implements K12DaoManager{   

    @PersistenceContext
    private EntityManager em;

//  @Autowired
//    private SessionFactory sessionFactory;
//
//    public void setSessionFactory(SessionFactory sessionFactory) {
//      this.sessionFactory = sessionFactory;
//  }

    @Override
    @Transactional(readOnly = true)
    public List<AttendanceBreakDown> getAttendanceBreakDownBySchoolAndGP(int school, String gradingPeriod, String gradeLevel) {

        Object values[] = new Object[]{new Integer(school), gradingPeriod, gradeLevel,
                                       new Integer(school), gradingPeriod, gradeLevel,
                                       new Integer(school), gradingPeriod, gradeLevel,
                                       new Integer(school), gradingPeriod, gradeLevel,
                                       new Integer(school), gradeLevel
                                      };
//        Call Named Query through JPA
//        Query query = em.createNamedQuery("attendanceDetailsBySchoolAndGradingPeriod");
//        
//        for (int i = 0; i < values.length; i++) {
//          query.setParameter(i, values[i]);
//        }
//        
//        List<AttendanceBreakDown> list = query.getResultList();
//        

//        Call Named Query through Hibernate's SessionFactory        
//        org.hibernate.Query query = sessionFactory.getCurrentSession().getNamedQuery("attendanceDetailsBySchoolAndGradingPeriod");
//        
//        for (int i = 0; i < values.length; i++) {
//          query.setParameter(i, values[i]);
//        }
//        
//        List<AttendanceBreakDown> list =  query.list();

        //Call Named Query through HibernateTemplate
        //List<AttendanceBreakDown> list =  getHibernateTemplate().findByNamedQuery("attendanceDetailsBySchoolAndGradingPeriod", values); 

        //return list;
        return null;
    }
}

Before, without using persistence.xml I had the typical hibernate.cfg.xml settings inside an applicationContext-datasource with it session factory, the session factory tied to the datasource etc.. an everything work fine.

Now I have persistence.xml, no more SessionFactory, EntityManager now.

How can I load hbm.xml files and execute them through Hibernate rather than through JPA 2.0 ?

If you see the commented code in the DAO If I was using a Hibernate config calling the named query through the HibernateTemplate (extending HibernateDaoSupport) was working. What would the code be like now ?

Thanks

解决方案

I have found the answer to my own question. To make it work this is what I have done:

  1. Use <mapping-file>...hbm.xml</mapping-file> inside <persistence-unit> in persistence.xml. I really did not have the use the <mapping-file>...hbm.xml</mapping-file> tag as that was giving me all sorts of exceptions one of them was DuplicateMappingException. According to the docs I also thought I had to use that tag, but it turns out that you do not have to.

  2. Created edu/kdc/visioncards/pojo/AttendanceBreakDown.hbm.xml under src/main/resources

  3. Finally in my DAO, I have as follows:

    @Override
    @Transactional(readOnly = true)
    public List<AttendanceBreakDown> getAttendanceBreakDownBySchoolAndGP(int school, String gradingPeriod, String gradeLevel) {
         Object values[] = new Object[]{new Integer(school), gradingPeriod, gradeLevel,
                                   new Integer(school), gradingPeriod, gradeLevel,
                                   new Integer(school), gradingPeriod, gradeLevel,
                                   new Integer(school), gradingPeriod, gradeLevel,
                                   new Integer(school), gradeLevel
                                  };
    
         org.hibernate.Session session = (Session) em.getDelegate();
         org.hibernate.Query query = session.getNamedQuery("attendanceDetailsBySchoolAndGradingPeriod");
         for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
         }
         List<AttendanceBreakDown> list =  query.list();
         return list;
    }
    

Now I can use JPA 2.0 through EntityManager and step down to Hibernate's Session to have access to all Hibernate features that JPA 2.0 does not offer.

这篇关于如何在JPA 2.0项目中加载Hibernate的'xxx.hbm.cfg'文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆