在执行查询时得到以下异常(Spring Boot + JPA + Hibernate)

时间:2017-10-31 10:31:01

标签: spring hibernate jpa spring-boot

这是尝试执行示例查询时收到的错误。请帮我摆脱这个。

org.hibernate.hql.internal.ast.QuerySyntaxException:未映射事务

下面的

是具有映射

的实体类

Transaction.java

package org.npst.mb.entity;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

@Entity
@Table(name="transaction")
public class Transaction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="transactionid")
    private int transactionid;  
    @Column(name="appid")
    private int appid;
    public int getTransactionid() {
        return transactionid;
    }
    public void setTransactionid(int transactionid) {
        this.transactionid = transactionid;
    }
    public int getAppid() {
        return appid;
    }
    public void setAppid(int appid) {
        this.appid = appid;
    }  
}

安装.java(Spring Boot App Stater)

 package org.npst.mb.install;

import org.npst.mb.controller.JobLauncherController;
import org.npst.mb.dao.TransactionDao;
import org.npst.mb.service.TransactionService;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@EnableBatchProcessing
@ImportResource("classpath:batchjob.xml")
@ComponentScan(basePackages ={"org.npst.mb.entity.*"},basePackageClasses= {TransactionService.class,TransactionDao.class,JobLauncherController.class})
public class Install {
    public static void main(String[] args) {
        SpringApplication.run(Install.class, args);


    }
}

DAOIMPL(查询与DB交互)

package org.npst.mb.dao.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.npst.mb.dao.TransactionDao;
import org.npst.mb.entity.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository
public class TransactionDaoImpl implements TransactionDao {
    @PersistenceContext 
    private EntityManager entityManager;    
    @Override
    public boolean addTransaction(Transaction txndata) {

        return true;
    }
    @Override
    public long getMaxTid() {
        try {
            System.out.println("maxtxnid::");
            String hql="from Transaction where transactionid > 0";                   
            entityManager.createQuery(hql).getMaxResults();

            return 0;
        }
        catch(Exception sql) {
            System.out.println(sql.getMessage());
            sql.printStackTrace();
            return 0;
        }
    }

}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test1
spring.datasource.username=root
spring.datasource.password=naveen123
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.data.jpa.repositories.enabled=true
spring.jpa.open-in-view=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 

1 个答案:

答案 0 :(得分:0)

据我了解,@ComponentScan会为注释@Component, @Service, @Controller,@Repository的类扫描并创建bean。

@EntityScan注释用于标识JPA实体(持久类)。

您可以尝试添加@EntityScan("org.npst.mb.entity")吗?