Spring MVC+;Hibernate:未知实体
我是Java世界的新手,特别是在春天。我试着读了很多教程,但我真的很困惑。 我创建了一些通过注释映射的实体,当我在一个主类中加载我的应用程序上下文时,我成功地将其持久化,但我不能在我的控制器中这样做Spring MVC+;Hibernate:未知实体,hibernate,spring-mvc,sprite,Hibernate,Spring Mvc,Sprite,我是Java世界的新手,特别是在春天。我试着读了很多教程,但我真的很困惑。 我创建了一些通过注释映射的实体,当我在一个主类中加载我的应用程序上下文时,我成功地将其持久化,但我不能在我的控制器中这样做 @Entity @Table(name = "User") public class User implements Serializable{ @Id @GeneratedValue @Column(name = "id_user") private Long i
@Entity
@Table(name = "User")
public class User implements Serializable{
@Id
@GeneratedValue
@Column(name = "id_user")
private Long idUser;
@Column(name = "first_name", nullable=false)
private String firstName;
@Column(name = "last_name", nullable=false)
private String lastName;
@Column(name = "email", nullable=false)
private String email;
@Column(name = "password", nullable=false)
private String password;
@Column(name = "birth")
private Date birth;
@Column(name = "avatar")
private String avatar;
...
}
我必须让sur使用javax.persistence
在dispachtcherservlet中,我加载spring-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.manager.spring" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<import resource="classpath*:spring-config.xml" />
</beans>
还有我的UserDAO
@Component("userDAO")//This the id by which it will be auto wired
public class UserDAO extends BaseDAO{
public void create(User user){
try{
getSession().save(user);
}
catch(HibernateException e){
System.out.println("Error occured while saving data"+e.getMessage());
}
}
}
BaseDAO:
public class BaseDAO {
@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession(){
return sessionFactory.openSession();
}
}
当我在一个主类中创建一个用户时,这个配置正在运行
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
UserService userSvc = (UserService) ctx.getBean("userSvc");
userSvc.create(user);
但是当我尝试在控制器中创建用户时,我得到了一个异常
Error occured while saving dataUnknown entity: com.manager.spring.entity.User
我真的希望有人能帮助我,我不一定什么都懂,我很抱歉我的英语不好!
Thx您显示了
事件
实体,但它却抱怨您的用户
实体。噢,该死!对不起,我已经编辑过了。spring config.xml
是指spring context.xml
,还是反之亦然?我还想看BaseDAO
。我真的不知道。。。我觉得我对春天很困惑。。。我在这个问题上添加BaseDao。
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
UserService userSvc = (UserService) ctx.getBean("userSvc");
userSvc.create(user);
Error occured while saving dataUnknown entity: com.manager.spring.entity.User