【发布时间】:2021-08-04 01:07:15
【问题描述】:
我正在使用带有配置类的 spring boot 和 hibernate。我的实体没有被映射。请参阅下面的错误。在查看了其他一些关于此的 stackoverflow 页面后,我仍然无法弄清楚。
我认为以下是正确的:HQL、@Entity、@Table
错误。
org.hibernate.hql.internal.ast.QuerySyntaxException: Message is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
实体。
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name = "strunk.entities")
@Table(name = "Message")
public class Message {
code...
}
DAO 类
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import strunk.entities.Message;
@Repository
public class MessageRepoImpl implements MessageRepo {
@Autowired
private SessionFactory sf;
@Override
public List<Message> getMessages() {
Session sess = sf.openSession();
List<Message> messages = null;
try {
messages = sess.createQuery("FROM Message").list();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
sess.close();
}
return messages;
}
more code...
}
当我将 getMessages 方法体替换为以下内容时,它可以工作
Session sess = sf.openSession();
List<Message> messages = sess.createNativeQuery(
"SELECT * FROM Message" )
.list();
return messages;
数据库表名是'Message'。
SpringBoot 类
@SpringBootApplication(exclude= HibernateJpaAutoConfiguration.class)
@ComponentScan("strunk")
@EnableJpaRepositories("strunk")
@EntityScan("strunk")
public class InstantMessengeTranslatorApplication {
public static void main(String[] args) {
SpringApplication.run(InstantMessengeTranslatorApplication.class, args);
}
}
休眠配置类
package strunk.config;
import java.io.FileInputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfig {
@Value("${spring.datasource.driver-class-name}")
private String driverClass;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${hibernate.dialect}")
private String dialect;
@Bean
public DataSource getDatasource(){
DriverManagerDataSource datasource = new DriverManagerDataSource(url, username, password);
datasource.setDriverClassName(driverClass);
return datasource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", dialect);
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
return properties;
}
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(getDatasource());
factory.setHibernateProperties(hibernateProperties());
factory.setPackagesToScan(new String[] {"com.xadmin.springboothibernate.model"});
return factory;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory factory)
{
HibernateTransactionManager transactionManger = new HibernateTransactionManager();
transactionManger.setSessionFactory(factory);
return transactionManger;
}
}
我错过了什么?
【问题讨论】:
-
您应该在会话工厂bean初始化中添加要扫描的实体包
-
我不完全确定你的意思。你是在建议我在 HibernateConfig 类中做点什么吗?
标签: java spring-boot hibernate hql hibernate-mapping