我有兩個實體類。用戶和FriendStatus.Friend狀態保存有關來自其他用戶的朋友請求的數據。hibernate @onetomany雙向映射不能正確映射數據
用戶:
@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目的是它的工作原理fine.But當我保存的對象的第二時間以相同requestAcceptor對象的數據庫,休眠從request_acceptor_id列刪除先前的id並將其寫入新行。請幫助我。
編輯:
這是我將我的對象保存到數據庫的方法。
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;
}
這是我調用創建方法的方法:
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 |
+----+----------------+---------------------+-------------------+
當我保存FriendStatus對象(它來自fr om客戶端)與requestSender其中ID爲29和requestAcceptor對象的ID爲23 Hibernate將其保存到ID爲18的列。之後,當我保存第二個FriendStatus對象與requestSender其中ID爲30和requestAcceptor對象的ID爲23,休眠將id爲18的行中的NULL替換爲request_acceptor_id,然後在db中創建新的行,其中request_acceptor_id是23.But我希望當我添加第二個對象時,第一個對象不會改變。我不想用request_acceptor_id替換NULL當我創建新的列與相同的request_acceptor_id。
安置自己的代碼中刪除級聯規則,並明確說明您認爲是什麼做的,它做什麼代替。 –
我編輯的問題。請說如果我應該添加一些東西。 –
我已經說過了:發佈你的代碼,並清楚地解釋你期望它做什麼,以及它做了什麼。 –