【发布时间】:2014-02-10 16:50:37
【问题描述】:
我正在使用管理酒店预订的 netbeans 开发 EJB 应用程序。我意识到实体管理器的 em.merge() 函数会在数据库中插入一条新记录,而不是在实体的主键或 @Id 设置为自动生成时进行更新。 我有两个实体 - 预订和房间。 Booking 的 ID 是自动生成的,而 Room 的 ID 不是自动生成的。会话 bean 中的相同 merge() 函数为 Booking 插入新行,但为 Room 更新。
我的实体bean和会话bean如下:-
预订实体
@SequenceGenerator(name="booking_seq", initialValue=1, allocationSize=100)
@Entity
@NamedQueries({@NamedQuery(name="Booking.getAll",query="SELECT e FROM Booking e order by e.bookingId")})
public class Booking implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="booking_seq")
@Column
private int bookingId;
@Column
private int roomId;
@Column
private int customerId;
@Column
@Temporal(javax.persistence.TemporalType.DATE)
private Date arrival_date;
@Column
@Temporal(javax.persistence.TemporalType.DATE)
private Date departure_date;
public Booking(int bookingId, int roomId, int customerId, Date arrival_date, Date departure_date) {
this.bookingId = bookingId;
this.roomId = roomId;
this.customerId = customerId;
this.arrival_date = arrival_date;
this.departure_date = departure_date;
}
public Booking() {
}
public int getBookingId() {
return bookingId;
}
public void setBookingId(int bookingId) {
this.bookingId = bookingId;
}
public int getRoomId() {
return roomId;
}
public void setRoomId(int roomId) {
this.roomId = roomId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public Date getArrival_date() {
return arrival_date;
}
public void setArrival_date(Date arrival_date) {
this.arrival_date = arrival_date;
}
public Date getDeparture_date() {
return departure_date;
}
public void setDeparture_date(Date departure_date) {
this.departure_date = departure_date;
}
}
房间实体
@Entity
@Table
@NamedQueries({@NamedQuery(name="Room.getAll",query="SELECT e FROM Room e order by e.roomId")})
public class Room implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int roomId;
@Column
private String roomType;
@Column
private String bedType;
@Column
private double tariff;
public Room() {
}
public Room(int roomId, String roomType, String bedType, double tariff) {
this.roomId = roomId;
this.roomType = roomType;
this.bedType = bedType;
this.tariff = tariff;
}
public int getRoomId() {
return roomId;
}
public void setRoomId(int roomId) {
this.roomId = roomId;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public String getBedType() {
return bedType;
}
public void setBedType(String bedType) {
this.bedType = bedType;
}
public double getTariff() {
return tariff;
}
public void setTariff(double tariff) {
this.tariff = tariff;
}
}
预订实体的会话 bean
@Stateless
public class BookingDAO implements BookingDAOLocal {
@PersistenceContext
private EntityManager em;
@Override
public void addBooking(Booking booking) {
em.persist(booking);
}
@Override
public void editBooking(Booking booking) {
em.merge(booking);
}
@Override
public void deleteBooking(int bookingId) {
em.remove(em.find(Booking.class, bookingId));
}
}
房间实体的会话 bean
@Stateless
public class RoomDAO implements RoomDAOLocal {
@PersistenceContext
private EntityManager em;
@Override
public void addRoom(Room room) {
em.merge(room);
em.flush();
}
@Override
public void editRoom(Room room) {
em.merge(room);
em.flush();
}
@Override
public void deleteRoom(int roomId) {
em.remove(em.find(Room.class, roomId));
}
}
【问题讨论】:
-
您尝试合并的预订已经有一个有效的 ID 吗?
-
我们需要看看你如何获取和填充传递给
merge()的对象。 -
其实我现在得到了答案。对于 editBooking() 方法,我使用与 addBooking() 相同的代码。在 addBooking() 我没有 setBookingId() 方法调用,因为它是自动生成的。只需要为编辑方法添加额外的部分。我在答案中添加的修改代码。
标签: jpa entitymanager