java - 在使用 Spring + Hibernate JPA 时不添加 persistence.xml 时使用 native SQL 实体

标签 java spring hibernate spring-mvc jpa

Please consider the question closed. It seems my usecase does not have a solution while working with JPA 2.0. I had overlooked limitations of JPA 2.0 in this case. JPA 2.0 does not support a custom entity without using persistence.xml.

我正在尝试测试我的应用程序是否可以与 Spring + Hibernate JPA 一起使用,同时避免使用专用的 persistence.xml。完全使用注释。

我使用的 API 是 IBM WebSphere 8.0 上的 Spring 4.3.3 和 Hibernate JPA-2.0

我不知道我的用例可能存在差距。这也可能是我的一个简单的失误。

目标是执行 native 查询并将结果映射到 bean 类。 我可以使用实体测试我的应用程序。 JPA 能够根据实际数据库表验证它们的映射。

为什么使用原生查询而不编写完整的实体 -> 查询涉及多个连接。不需要将查询中涉及的所有表作为实体包含在内。

与 jpa 相关的应用程序上下文 xml 的一部分如下所示

<bean id="jpaVendorApdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect"></property>
    <property name="showSql" value="true" />
</bean>

<bean id="testEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="testDB2DataSource" />
  <property name="packagesToScan" value="com.app.dao" />
  <property name="jpaVendorAdapter">
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  </property>
  <property name="jpaProperties">
     <props>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
     </props>
  </property>

为我的案例添加一个简单的示例 用于 native 查询的 bean 类如下所示

public class OwnerDetails implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private String userInitials;

    private String teamCd;

    public String getUserInitials() {
        return userInitials;
    }

    public void setUserInitials(String userInitials) {
        this.userInitials= userInitials;
    }

    public String getTeamCd() {
        return teamCd;
    }

    public void setTeamCd(String teamCd) {
        this.teamCd= teamCd;
    }
}

查询为

SELECT B.USER_INITS as userInitials, B.TEAM_CD as teamCd FROM APPTEAM A JOIN PQID B ON A.APP_OWNER_ID = B.SIGN_ON_ID WHERE A.APP_NAME = :appName

执行 native 查询的代码块

Query query = entityManager.createNativeQuery(queryString, className)(QUERY_STRING, OwnerDetails.class);
    query.setParameter("appName", "TESTAPP");


    OwnerDetails  result = (OwnerDetails ) query.getSingleResult();

尝试执行查询时出现以下异常

Caused by: org.hibernate.MappingException: Unknown entity: com.app.dao.entity.OwnerDetails

我知道这是因为不包含 @Entity 注释 但是当我包含实体注释时,我收到了不同的错误

Caused by: org.hibernate.HibernateException: Missing table: OwnerDetails

......

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory

我想知道在不使用 persistence.xml 的情况下设置 JPA 的缺失链接

Update - as the API is JPA 2.0 Named Native Queries would not be supported

最佳答案

尝试这些步骤。

  1. @Entity注释放入您的类中
  2. 在类中添加@Table注释

     @Entity
     @Table(name="YourTableName") //if you do not put name, the dafault value is your class name
     public class OwnerDetails implements Serializable {
     }
    
  3. 确保您的表是否存在。

编辑:

如果 OwnerDetails 不是实体并且您使用的是 JPA 2.1,那么您可以使用 SqlResultSetMapping with ConstructorResult .

关于java - 在使用 Spring + Hibernate JPA 时不添加 persistence.xml 时使用 native SQL 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539347/

相关文章:

java - ACM编程题

java - AWS 单实例 Tomcat SSL

java - 使用 Ant 运行 JUnit 测试

java - Spring @Transactional 注释使我的类无法 Autowiring

hibernate - Hibernate (HQL) 是否支持公用表表达式

java - 使 JTable 单元格编辑器值可选择,但不可编辑?

java - 如何通过 spring RestTemplate 更改获取请求中的响应 http header ?

spring - 仅为mongodb创建ssl连接

java - Spring Boot JPARepository 在 save() 上的性能

hibernate - 如何在Grails中使用注入(inject)的dataSource来执行操作?