我有两个实体作为用户和项目,它们之间具有@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=?