2
回答
hibernate和springMVC整合时出现的问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我在用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的时候是怎么解决这个冲突的啊!

举报
顶部