hibernate和springMVC整合时出现的问题

专打小盆友 发布于 2011/10/30 23:54
阅读 1K+
收藏 1

我在用spring和hibernate的时候写了一个一对多关联的entity的时候报错了,

Group:

@Entity
public class Group implements Serializable{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private int id;
 private String name;
 private Set<User> users;
 @Id
 @GeneratedValue
 @Column(name="group_id")
 public int getId() {
  return id;
 }
 @Column(name="group_name")
 public String getName() {
  return name;
 }
 @OneToMany(mappedBy="group")
 public Set<User> getUsers() {
  return users;
 }
 public void setId(int id) {
  this.id = id;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void setUsers(Set<User> users) {
  this.users = users;
 }
}

User:

@Entity
public class User implements Serializable{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private int id;
 private String name;
 private String password;
 private Group group;
 @ManyToOne
 public Group getGroup() {
  return group;
 }
 @Id
 @GeneratedValue
 @Column(name="user_id")
 public int getId() {
  return id;
 }
 @Column(name="user_name")
 public String getName() {
  return name;
 }
 @Column(name="user_password")
 public String getPassword() {
  return password;
 }
 public void setGroup(Group group) {
  this.group = group;
 }
 public void setId(int id) {
  this.id = id;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void setPassword(String password) {
  this.password = password;
 }
}

UserDAOImpl

@Repository
public class UserDaoImpl implements UserDao {
 private HibernateTemplate hibernateTemplate;
 public HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }
 @Autowired
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  this.hibernateTemplate = hibernateTemplate;
 }
 public void save(User user) {
  
  hibernateTemplate.save(user);
 }
 public User getUserById(int id) {
  User user=(User)hibernateTemplate.get(User.class, id);
  return user;
 }
 public User getUserByName(String name) {
  // TODO Auto-generated method stub
  return null;
 }

}

UserService

@Service
@Transactional
public class UserService {
 private UserDao userDao;
 private LogDao logDao;
 
 public void add(User user){
  userDao.save(user);
  Log log=new Log();
  log.setMsg("user saveed!");
  logDao.save(log);
 }
 public User getUserByid(int id){
  return userDao.getUserById(id);
 }
 public LogDao getLogDao() {
  return logDao;
 }
 public UserDao getUserDao() {
  return userDao;
 }
 @Autowired
 public void setLogDao(LogDao logDao) {
  this.logDao = logDao;
 }
 @Autowired
 public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
 }
}

测试的时候test.java

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  UserService userservice = (UserService) ctx.getBean("userService");
  userservice.getUserByid(1);

报错:nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z

网上查到有人说是hibernate的jpa_api那个包跟mycelipse自带的spring persistence包的那个@OneToMany和@ManyToOne冲突了,这个大家怎么解决的啊,我spring引入了myeclipse的spring的AOP,Core,persistence,和WEb 这几个包!大家用spring和hibernate的时候是怎么解决这个冲突的啊!

加载中
0
铂金便便
铂金便便

见这个网址:

可以解答,大多数是asm.jar的问题

http://hyanqing1.iteye.com/blog/405329

专打小盆友
专打小盆友
我把asm-2.2.2.jar、asm-commons-2.2.2.jar、asm-util-2.2.2.jar、cglib-nodep-2.1_3.jar这四个JAR包引进去了还是报之前那个错~
0
-10
-10
试试把 Eclipse 引入的 Java EE 5 Library 改为 4
专打小盆友
专打小盆友
还是不行~~~
专打小盆友
专打小盆友
谢谢咯,我去试试看
返回顶部
顶部