3

我有两个实体作为用户和项目,它们之间具有@ManyToMany 关系,如下所示。

实体类

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "id")
    private int id;
    @Column (name = "email")
    private String email;
    @Column (name = "password")
        private String password;
    @Column (name = "last_login")
    private Date lastLogInDate; 
    @ManyToMany(fetch = FetchType.EAGER)
        @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
        @JoinTable(name = "project_assigned", joinColumns = { @JoinColumn(name = "fk_user_id")  }, inverseJoinColumns = { @JoinColumn(name = "fk_project_id") } )
    private Set<Projects> projects = new HashSet<Projects>(0);  

    // setter and getter methods.
}

@Entity
@Table(name = "projects")
public class Projects {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column (name = "name")
    private String name;    
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "user_projects", joinColumns = { @JoinColumn(name = "fk_project_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_user_id") })
    private Set<User> userSet = new HashSet<User>(0);   

}

DAO 类

@Repository
public class UserDAO {

    public User findByCredentials(String email, String password) {
        String queryString = " from User u where u.email = ? and u.password = ?";
        Query query = sessionFactory.getCurrentSession().createQuery(queryString);
        query.setString(0, email);
        query.setString(1, password);       
        return (User) query.uniqueResult();     
    }

    public boolean updateUser(User user) {
        boolean result = true;
        Session session = null;
        try {
            session = sessionFactory.getCurrentSession();
            session.update(user);
        } catch (HibernateException e) {
            result = false;
            logger.error("Can not Update User"+e);
            e.printStackTrace();
        } 
        return result;
    }           
}

服务等级

@Service
@Transactional(readOnly = true,propagation=Propagation.SUPPORTS)
public class UserService {

    @Autowired
    private UserDAO userDAO;

    public User findByCredentials(String userName, String lastName) {
        return userDAO.findByCredentials(userName, lastName);   
    }

    @Transactional(readOnly = false,propagation=Propagation.REQUIRED)
    public boolean updateUser(User user) {
        return userDAO.updateUser(user);
    }
}

其他代码

User user = userService.findByCredentials("xyz@pqr.com", "abc");
user.setLastLogInDate(new Date());
userService.updateUser(user);

我的问题是,当我更新用户的“LastLogInDate”时,分配给该用户的所有项目实体都将得到更新(不必要的触发更新语句),因为我的应用程序性能很低。我怎么解决这个问题。以及如何以更好的方式做到这一点。谢谢你的帮助。

这是我的 SQL 日志

DEBUG org.hibernate.SQL#log  - update users set email=?, password=?, last_login=? where id=?
DEBUG org.hibernate.SQL#log  - update user_projects set name=? where id=?
DEBUG org.hibernate.SQL#log  - update user_projects set name=? where id=?
DEBUG org.hibernate.SQL#log  - update user_projects set name=? where id=?
DEBUG org.hibernate.SQL#log  - update user_projects set name=? where id=?
DEBUG org.hibernate.SQL#log  - update user_projects set name=? where id=?
4

2 回答 2

2

您必须设置级联,因为在运行时无法设置级联(保存):

@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})

您至少必须删除CascadeType.MERGE. 因为 MERGE 意味着类似于“保存”的复杂事物,但更像是“将这个分离的实体推回托管状态并保存其状态更改”。级联意味着所有关联的实体都以相同的方式被推回,并且您从 .merge() 返回的托管实体句柄具有与之关联的所有托管实体。

于 2013-07-25T23:17:15.097 回答
-1

删除所有级联选项。这将解决您的问题

1 解决方案:

解决方案1:

用户类:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_projects", joinColumns = { @JoinColumn(name = "fk_project_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_user_id") })
private Set<Projects> projects;

项目类

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_projects", joinColumns = { @JoinColumn(name = "fk_project_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_user_id") })
private Set<User> userSet; 
于 2014-01-20T18:27:16.093 回答