一个困扰N天的问题:Unknown entity

用maven整合ssh2.遇到一个问题。在对实体做持久化时报错:
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: cn.lvlf.ssh.demo.model.User...
...
Caused by: org.hibernate.MappingException: Unknown entity: cn.lvlf.ssh.demo.model.User...
...

下面将代码贴出来希望能有人指点一下。不胜感谢!
applicationContext.xml:
[code="java"]
...










${hibernate.dialect}

false

org.hibernate.cache.EhCacheProvider

true





cn.lvlf.ssh.demo.model.*




...
[/code]

User.java
[code="java"]
package cn.lvlf.ssh.demo.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 = "t_user")
public class User implements java.io.Serializable {

private Long id;
private String name;
private String password;

public User() {
}

public User(String name, String password) {
    this.name = name;
    this.password = password;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
    return this.id;
}

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

@Column(name = "name")
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "password")
public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}
[/code]

数据库(MySql的):
t_user:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+

测试代码:
DaoTest.java
[code="java"]
public class DaoTest {

@Test
public void testUserDao(){
    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userDao = (UserDao)ctx.getBean("userDao");
    cn.lvlf.ssh.demo.model.User user = new User("zhangsan","sssss");
    userDao.saveUser(user);

    List<User> users = userDao.getAllUsers();
    Assert.assertTrue(users.size()>0);
}

}
[/code]

2个回答

可能是路径问题
例如:
你的实体存放目录是com.ss3.demo.entity.test,里面存放着很多实体类。
但是你的packagesToScan就要写成com.ss3.demo.entity.*,千万不要写成com.ss3.demo.entity.test.*,否则不会被映射,换句话说就是配置映射目录要比实际少一层就可以了

K532454550
K532454550 rt,thanks
6 年多之前 回复

这样试下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问