我有两个实体类.User和FriendStatus.Friend状态保存有关来自其他用户的朋友请求的数据。
用户:
@Entity
@Table(name = "USERS")
@XmlRootElement
public class User implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "acceptor")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Message> acceptedMessages;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "sender")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Message> sentMessages;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
@Column(name = "phone_number")
private String phoneNumber;
@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<User> friends;
@OneToMany(cascade = CascadeType.ALL,mappedBy="requestAcceptor")
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnoreProperties("requestAcceptor")
private List<FriendStatus> acceptedFriendRequests;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "requestSender")
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnoreProperties("requestSender")
private List<FriendStatus> sentFriendRequests;
@Column(name = "profile_status")
private String profileStatus;
@Enumerated(EnumType.STRING)
@Column(name = "activation_status")
private UserActivationStatus activationStatus;
FriendStatus:
@Entity
@Table(name="FRIEND_STATUS")
public class FriendStatus implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="request_sender_id")
@JsonIgnoreProperties("sentFriendRequests")
private User requestSender;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="request_acceptor_id")
@JsonIgnoreProperties("acceptedFriendRequests")
private User requestAcceptor;
@Enumerated(EnumType.STRING)
@Column(name = "request_status")
private FriendRequestStatus friendRequestStatus;
当我第一次将FriendStatus对象保存到数据库时,它工作正常。但是当我使用相同的requestAcceptor对象第二次保存对象时,hibernate从request_acceptor_id列中删除先前的id并将其写入新行.HELP ME PLEASE。< / p>
编辑:
这是我将对象保存到db的方法。
public T create(T object) {
T objectFromDB = object;
Session session = NewHibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = (Transaction) session.beginTransaction();
session.save(object);
transaction.commit();
} catch (HibernateException e) {
if (session != null){
session.getTransaction().rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return objectFromDB;
}
这是我调用create method的方法:
public void sendFriendRequest(FriendStatus object) {
FriendStatus status = fDao.create(object);//fDao is the object from Dao class which includes create method.
}
这是我的控制者:
@RequestMapping(value="/sendFriendRequest",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON,produces = MediaType.APPLICATION_JSON)
public String sendFriendRequest(@RequestBody FriendStatus status) {
serviceUser.sendFriendRequest(status);//serviceUser is the object from class that includes sendFriendRequest method.
return "OK";
}
这是我在db中的表:
+====+================+=====================+===================+
| id | request_status | request_acceptor_id | request_sender_id |
+====+================+=====================+===================+
| 18 | WAITING | NULL | 29 |
+----+----------------+---------------------+-------------------+
| 19 | WAITING | 23 | 30 |
+----+----------------+---------------------+-------------------+
当我使用id为29的requestSender保存FriendStatus对象(来自客户端)时,id为23的requestAcceptor对象hibernate将其保存到id为18的列。之后我用requestSender保存第二个FriendStatus对象的id是30和requestAcceptor对象,其id是23,hibernate用id为18的行替换request_acceptor_id,然后在db中创建新行,request_acceptor_id是23.但我想要当我添加第二个对象时,第一个对象不更改。当我使用相同的request_acceptor_id创建新列时,我不想将request_acceptor_id替换为NULL。
答案 0 :(得分:0)
与你的级联设置有关,
尝试使用以下内容保存:
public void sendFriendRequest(FriendStatus status) {
User requester=userDao.findOne(status.getRequestAcceptor.getId());
User sender=userDao.findOne(status.getRequestSender.getId());
......
statusDao.create(status);
status.getRequestAcceptor(requester);
status.getRequestSender(sender);
statusDao.update(status);
}
状态携带的用户具有私有List sentFriendRequests和私有List acceptedFriendRequests;设置为空列表
如果您的交易超出整个功能,它会更好,那么您可以使用: sender.getSentFriendRequests()。add(status)和reqester.getacceptedFriendRequests()。add(status);我想这是@OneToMany,Cascade.All
的重点否则你可以从集合中删除级联规则