我正在使用netbeans开发一个EJB应用程序来管理Hotel Bookings。我意识到实体管理器的em.merge()函数在数据库中插入一条新记录,而不是在主键或实体的@Id设置为自动生成时更新。 我有两个实体 - 预订和房间。预订的ID是自动生成的,而对于Room,它不是自动生成的。会话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));
}
}
Room Entity的会话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));
}
}
答案 0 :(得分:0)
其实我现在得到了答案。对于editBooking()方法,我使用与addBooking()相同的代码。在addBooking()中,我没有setBookingId()方法调用,因为它是自动生成的。只需要为编辑方法添加额外的部分。
else if ("Add".equalsIgnoreCase(action) || "Edit".equalsIgnoreCase(action) )
{
try {
arrival_date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(request.getParameter("arrival_date"));
departure_date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(request.getParameter("departure_date"));
}
catch(ParseException e) {
e.printStackTrace();
}
Booking booking = new Booking();
if("Edit".equalsIgnoreCase(action))
{
int bookingId=Integer.parseInt(request.getParameter("bookingId"));
booking.setBookingId(bookingId);
}
booking.setRoomId(Integer.parseInt(request.getParameter("roomId")));
booking.setCustomerId(customerId);
booking.setArrival_date(arrival_date);
booking.setDeparture_date(departure_date);
if("Add".equalsIgnoreCase(action))
bookingDao.addBooking(booking);
else
bookingDao.editBooking(booking);
request.setAttribute("allBookings", bookingDao.getAllBookings());
request.getRequestDispatcher("booking_details.jsp").forward(request, response);
}
答案 1 :(得分:0)
你没有尝试更新记录,你试图坚持同一个房间,而不是尝试这个。
@Override
public void editRoom(Room room) {
Room r-= em.merge(room);
r.setRoomType("2bed"); // your own update field other than the @Id (Primary key)
em.flush();
// you can retun the updated employee.
}