NamedQuery错误 - “具有给定标识符的多行:1”

时间:2016-02-16 11:51:13

标签: java jpa jboss

现在已经排除故障一段时间了,但我仍然有同样的错误。

我正在使用EJB& amp; JPA,但在尝试调用我的namedQuery时,我收到以下错误:

 javax.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: g24.isp.ejb.Reservation

之前我收到了同样的错误,但是通过将FetchType更改为LAZY来修复它。 这个想法是客户可以在不同的几周内预订同一个小屋,但似乎有些不对劲。 SQL语句“SELECT * FROM Cabin”的工作方式类似于MySQL中的一个魅力,但在使用命名查询时则不然。

以下是所涉及的课程:

小屋:

package g24.isp.ejb;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@NamedQueries({ @NamedQuery(name = "Cabin.findAll", query = "SELECT cb FROM Cabin cb"),
        @NamedQuery(name = "Cabin.findByCType", query = "SELECT c FROM Cabin c WHERE c.cType LIKE :cType"), })

@Table(name = "Cabin")
public class Cabin implements Serializable {

    @Id
    @Column(name = "cabinNo", nullable = false)
    private int cabinNo;

    @Column(name = "cType")
    private String cType;

    @ManyToOne
    @JoinColumn(name = "hID", referencedColumnName = "hotelID")
    private Hotel hotel;

    @OneToOne(mappedBy = "cabin")
    Reservation reservation;

.... GETTERS AND SETTERS BELOW (Cut out to save space)....

Cabin EAO:

package g24.isp.eao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import g24.isp.ejb.Cabin;
import g24.isp.ejb.Customer;

/**
 * Session Bean implementation class CabinEAO
 */
@Stateless
public class CabinEAO implements CabinEAOLocal {

    @PersistenceContext(unitName = "MysqlDS")
    private EntityManager em;

    public Cabin findByCabinNo(int cabinNo) {
        return em.find(Cabin.class, cabinNo);
    }

    public Cabin createCabin(Cabin cabin) {
        em.persist(cabin);
        return cabin;
    }

    public Cabin updateCabin(Cabin cabin) {
        em.merge(cabin);
        return cabin;
    }

    public void deleteCabin(int cabinNo) {
        Cabin cb = this.findByCabinNo(cabinNo);
        if (cb != null) {
            em.remove(cb);
        }
    }

    public List<Cabin> findAll() {
        TypedQuery<Cabin> query = em.createNamedQuery("Cabin.findAll", Cabin.class);
        List<Cabin> results = query.getResultList();
        return results;
    }

    public List<Cabin> findByCabinType(String cType) {
        TypedQuery<Cabin> query = em.createNamedQuery("Cabin.findByCType", Cabin.class);
        query.setParameter("cType", cType);
        List<Cabin> cabins = query.getResultList();
        return cabins;

    }

    /**
     * Default constructor.
     */
    public CabinEAO() {
        // TODO Auto-generated constructor stub
    }

}

预订课程:

package g24.isp.ejb;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@SequenceGenerator(name = "ID_SEQ", allocationSize = 1)
@Table(name = "Reservation")
public class Reservation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "resID")
    private long resID;

    @Column(name = "rDate", nullable = false)
    private int rDate;

    @OneToOne
    @JoinColumn(name = "cNo", referencedColumnName = "cabinNo", nullable = false)
    private Cabin cabin;

    @ManyToOne
    @JoinColumn(name = "cPnr", referencedColumnName = "cPnr", nullable = false)
    private Customer customer;

 .... GETTERS and SETTERS ....

SERVLET:

package g24.isp.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import g24.isp.ejb.Cabin;
import g24.isp.ejb.Customer;
import g24.isp.ejb.Hotel;
import g24.isp.ejb.Reservation;
import g24.isp.facade.Facade;
import g24.isp.facade.FacadeLocal;

/**
 * Servlet implementation class HotelServlet
 */
@WebServlet("/HotelServlet")
public class HotelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private FacadeLocal facade;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HotelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html><html><head>");
        out.println("<title>Lab5</title>");
        out.println("<meta charset=\"ISO-8859-1\">");
        out.println("</head><body>");
        out.println("<h3>Hotell hejhej test: </h3>");

        // Hämta alla Employees
        out.println("<br><h3>Alla Cabins:</h3>");

        List<Cabin> allCabins = facade.findAll();
        // Skriv ut alla
        for (Cabin c1 : allCabins) {
            out.println("<h4>Hittade: " + c1.getClass().getSimpleName());
            out.println(" Id: " + c1.getCabinNo());
            out.println(" - " + c1.getcType());
        }
    }
}

FACADE:

package g24.isp.facade;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import g24.isp.eao.CabinEAOLocal;
import g24.isp.eao.CustomerEAOLocal;
import g24.isp.eao.HotelEAO;
import g24.isp.eao.HotelEAOLocal;
import g24.isp.eao.ReservationEAOLocal;
import g24.isp.ejb.Cabin;
import g24.isp.ejb.Customer;
import g24.isp.ejb.Hotel;
import g24.isp.ejb.Reservation;

/**
 * Session Bean implementation class Facade
 */
@Stateless
public class Facade implements FacadeRemote, FacadeLocal {

    @EJB
    CustomerEAOLocal customerEAO;

    @EJB
    CabinEAOLocal cabinEAO;

    @EJB
    HotelEAOLocal hotelEAO;

    @EJB
    ReservationEAOLocal reservationEAO;

    public Facade() {
        // TODO Auto-generated constructor stub
    }

    /*
     * CUSTOMER METHODS
     */

    public Customer findByCpnr(String cPnr) {
        return customerEAO.findByCpnr(cPnr);
    }

    public Customer createCustomer(Customer customer) {
        return customerEAO.createCustomer(customer);
    }

    public Customer updateCustomer(Customer customer) {
        return customerEAO.updateCustomer(customer);
    }

    public void deleteCustomer(String cPnr) {
        customerEAO.deleteCustomer(cPnr);
    }

    /*
     * CABIN METHODS
     */

    public Cabin findByCabinNo(int cabinNo) {
        return cabinEAO.findByCabinNo(cabinNo);
    }

    public Cabin createCabin(Cabin cabin) {
        return cabinEAO.createCabin(cabin);
    }

    public Cabin updateCabin(Cabin cabin) {
        return cabinEAO.updateCabin(cabin);
    }

    public void deleteCabin(int cabinNo) {
        cabinEAO.deleteCabin(cabinNo);
    }

    public List<Cabin> findAll() {
        return cabinEAO.findAll();

    }

    public List<Cabin> findCabinsByType(String cType) {
        return cabinEAO.findByCabinType(cType);
    }
    /*
     * HOTEL METHODS
     */

    public Hotel findByHotelID(int hotelID) {
        return hotelEAO.findByHotelID(hotelID);
    }

    public Hotel createHotel(Hotel hotel) {
        return hotelEAO.createHotel(hotel);
    }

    public Hotel updateHotel(Hotel hotel) {
        return hotelEAO.updateHotel(hotel);
    }

    public void deleteHotel(int hotelID) {
        hotelEAO.deleteHotel(hotelID);
    }

    /*
     * RESERVATION METHODS
     */
    public Reservation findByReservationID(long resID) {
        return reservationEAO.findByReservationID(resID);
    }

    public Reservation createReservation(Reservation reservation) {
        return reservationEAO.createReservation(reservation);
    }

    public Reservation updateReservation(Reservation reservation) {
        return reservationEAO.updateReservation(reservation);
    }

    public void deleteReservation(long resID) {
        reservationEAO.deleteReservation(resID);
    }

}

我选择不包括客户,酒店以及本地课程(EAOLocal&amp; Facade)。

我在这里一无所知,但不应该这样吗?我选择不在预订中使用复合键,因为它应该能够识别Cabin&amp;客户只需使用各自表中的外键。

0 个答案:

没有答案