开发者社区> 问答> 正文

Spring整合Hibernate出错?错误为unknown entity!?报错

这是项目的目录结构:

这是User类:

package com.baofan.shtest.model;

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
@Table(name="user")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id", length=32)
    private Integer id;
    @Column
    private String username;
    @Column
    private String password;
    @Column
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}



这是UserDao类:

package com.baofan.shtest.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.baofan.shtest.model.User;

@Repository
public class UserDao {
	@Autowired
    @Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public User get(int id) {
    	Session session = sessionFactory.getCurrentSession();
    	session.beginTransaction();
    	User user = (User)session.get(User.class, new Integer("1"));
    	session.getTransaction().commit();
    	return user;
    }
}



这是UserService类:

package com.baofan.shtest.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baofan.shtest.dao.UserDao;
import com.baofan.shtest.model.User;

@Service
public class UserService {
	@Autowired
    private UserDao userDao;
 
    public UserDao getGoodsDao() {
        return userDao;
    }
 
    public void setGoodsDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    public User getUser() {
		return this.userDao.get(1);
	}
}



这是Controller类:

package com.baofan.shtest.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.baofan.shtest.service.UserService;

@Controller
public class ShowController {
	@Resource(name="userService")
	private UserService userService;
	
	@RequestMapping("/show")
	public ModelAndView show() {
		ModelAndView mav = new ModelAndView("show");
		mav.addObject("user", userService.getUser());
		return mav;
	}
}



Spring 上下文配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
	xmlns:tx="http://www.springframework.org/schema/tx">


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/batis?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.query.substitutions">true 1, false 0</prop>
				<prop key="hibernate.default_batch_fetch_size">16</prop>
				<prop key="hibernate.max_fetch_depth">2</prop>
				<prop key="hibernate.generate_statistics">true</prop>
				<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>



Spring  MVC核心Servlet配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 应用上下文配置  -->

	<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
	<mvc:annotation-driven />

	<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
	<context:component-scan base-package="com.baofan.shtest" />
	
	
	<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>




这是数据表:

然后访问show的时候报错:

不知道怎么改动。。。。求解答!

展开
收起
爱吃鱼的程序员 2020-06-10 10:47:45 784 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    User类中@id的注解需要写在对应的get***方法上

    p.s.其他可选注解也要写在对应get***方法上

    hibernate未配置,无法自动扫描并装载entity对象,虽然对象存在,并加了注解 packagesToScan扫描你的对象啊。不扫描怎么会知道?回复 @隔壁老鱼:我把jdbc的配置写到了配置文件里了,但是这样就出错了。。这个是问题:http://www.oschina.net/question/2272241_2142659回复 @隔壁老鱼:谢谢哦,都添加了,已经可以了 @击沉应该有2个,一个是hibernate的,一个是javapersistence的那个包有回复 @击沉:添加javax.persistence啊

    第一次运行的的时候,看控制台输出的信息总是有错误:

    说是classnotfound!!!

    给实体对象加个无参的构造函数默认不就是有一个无参构造函数
    2020-06-10 10:48:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多