Java 考虑定义一个类型的豆和*x27;org.hibernate.SessionFactory';在您的配置中
我是JavaEE的新手,一直在从事simlpe Springboot项目。 每次运行时都会出现以下错误: 请随时回答我的问题,我们非常感谢代码中的任何改进Java 考虑定义一个类型的豆和*x27;org.hibernate.SessionFactory';在您的配置中,java,spring,hibernate,Java,Spring,Hibernate,我是JavaEE的新手,一直在从事simlpe Springboot项目。 每次运行时都会出现以下错误: 请随时回答我的问题,我们非常感谢代码中的任何改进 Field sessionFactory in com.example.dao.CartDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found. Action: Consider defining a bean of ty
Field sessionFactory in com.example.dao.CartDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
Action:
Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
我看到您的项目,发现您的项目中没有用于
SessionFactory
的SessionFactory
类或配置,您正在CartDaoImpl
clas中使用@Autowired
用于SessionFactory
。这是主要问题。您需要配置SessionFactory
您可以参考以下示例:
- 在应用程序类中添加SessionFactorybean李>
- 在application.properties中添加当前会话上下文类李>
- 使用@Autowired注释使用SessionFactory
- 添加到application.properties中
spring.jpa.properties.hibernate.current\u session\u context\u class=org.springframework.orm.hibernate4.SpringSessionContext
再加上这个
spring.datasource.url=......
spring.datasource.username=....
spring.datasource.password=.....
spring.jpa.properties.hibernate.dialect=.......
spring.jpa.hibernate.ddl-auto=update
仅此而已: 1#例如,您在UserServiceImpl中使用
@Component
@Service
public class UserServiceImpl implements UserService {...
....
}
仅使用@Component或@Service,但不能同时使用,因为这是多余的。服务是一个组件
2#在只读取操作us而不是默认的@Transactional
的方法中,此@Transactional(readOnly=true)
三,#
在void addCustomerOrder(CustomerOrder-CustomerOrder)等方法中代码>最好返回布尔值或类似CustomerOrder的对象,而不是只返回void
4#类查询不可序列化
5#最好使用lazy作为默认值,而不是fetch=FetchType.EAGER
6#dao类CartDaoImpl依赖于服务类,这很奇怪
7#在某些情况下,您在dao级别上有事务,而在另一个服务级别上有事务
8#如果您可以创建子包impl并将所有实现移到一个包中
您将拥有具有N个接口的com.dao和具有N个实现的com.dao.impl,而不是一个具有N+N个intercases和类的包com.dao
将其添加到pom.xml中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
org.springframework.boot
可以从EntityManager获取会话,该会话已在spring boot starter data jpa
中配置。因此,插入EntityManager而不是SessionFactory:
@Autowired
private EntityManager entityManager;
private Session getSession() {
return entityManager.unwrap(Session.class);
}
并在需要时使用getSession()方法。创建服务并使用它从EntityManager获取会话:
@Service
public class HibernateService {
@PersistenceContext
private EntityManager entityManager;
public <R> NativeQuery<R> createNativeQuery(String sqlString, Class<R> resultClass) {
return getSession().createNativeQuery(sqlString, resultClass);
}
public NativeQuery createNativeQuery(String sqlString) {
return getSession().createNativeQuery(sqlString);
}
public Session getSession() {
return entityManager.unwrap(Session.class);
}
}
@服务
公共类HibernateService{
@持久上下文
私人实体管理者实体管理者;
公共NativeQuery createNativeQuery(字符串sqlString,类resultClass){
返回getSession().createNativeQuery(sqlString,resultClass);
}
公共NativeQuery createNativeQuery(字符串sqlString){
返回getSession().createNativeQuery(sqlString);
}
公共会话getSession(){
返回entityManager.unwrap(Session.class);
}
}
您可以添加更多方法来减少编写的代码量。使用Hibernate 5简单检查以下内容
--检查基本包是否已扫描(HibernateConfig.java)
--检查文件中的有效注释
@配置
@启用事务管理
--检查是否创建了所有有效的bean
LocalSessionFactoryBean、HibernateTransactionManager、HibernateTemplate您可以通过java代码进行配置,也可以使用xml
和属性文件进行配置。一切由你决定。如果您想使用BeanConfig.java,可以对其进行配置。我建议您使用xml配置我尝试过,但我无法修复错误,因为您下载了项目并成功修复了错误。您可以为我上传它吗?参考此项目并尝试配置它:或者您也可以参考此链接谢谢您的回答,我对配置部分(您提到的第一部分)非常感兴趣。如果你能在提出建议后用这些文件更新你的答案,那就太好了。我尝试了我自己,但我得到了更多的错误:(看我的更新。如果你有任何异常显示它