2

我正在尝试在列表中显示啤酒表中的所有数据对象,并在向下钻取中显示所有啤酒数据及其来自类别表的相应类别名称和来自样式表的相应样式名称,它会获取这些来自 beer 表样式 id 和类别 id 列的结果。

我尝试使用如图所示的 @SecondaryTables 注释,但在啤酒列表中它只显示 10 种啤酒,这是类别名称的数量,因此它搞乱了它的映射方式。以下是我这样做的尝试。

这是我添加到 JPA 类的内容,对此的正确解决方案是什么。

 @Table(name = "beers")
        @SecondaryTables({
        @SecondaryTable(name = "categories"),
        @SecondaryTable(name = "styles")})


@Column(table = "categories", name = "cat_name")
private String catName;

@Column(table = "styles", name = "style_name")
private String styleName;



  public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

这是 ER 图中表格的图片 在此处输入图像描述

这是 beers.java JPA 供额外参考

@Entity
@Table(name = "beers")
@SecondaryTables({
    @SecondaryTable(name = "categories"),
    @SecondaryTable(name = "styles")})
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Beers.findAll", query = "SELECT b FROM Beers b"),
    @NamedQuery(name = "Beers.findById", query = "SELECT b FROM Beers b WHERE b.id = :id"),
    @NamedQuery(name = "Beers.findByLikeName", query = "SELECT b FROM Beers b WHERE b.name LIKE CONCAT('%',:name,'%')"),
    @NamedQuery(name = "Beers.findByBreweryId", query = "SELECT b FROM Beers b WHERE b.breweryId = :breweryId"),
    @NamedQuery(name = "Beers.findByName", query = "SELECT b FROM Beers b WHERE b.name = :name"),
    @NamedQuery(name = "Beers.findByCatId", query = "SELECT b FROM Beers b WHERE b.catId = :catId"),
    @NamedQuery(name = "Beers.findByStyleId", query = "SELECT b FROM Beers b WHERE b.styleId = :styleId"),
    @NamedQuery(name = "Beers.findByAbv", query = "SELECT b FROM Beers b WHERE b.abv = :abv"),
    @NamedQuery(name = "Beers.findByIbu", query = "SELECT b FROM Beers b WHERE b.ibu = :ibu"),
    @NamedQuery(name = "Beers.findBySrm", query = "SELECT b FROM Beers b WHERE b.srm = :srm"),
    @NamedQuery(name = "Beers.findByAddUser", query = "SELECT b FROM Beers b WHERE b.addUser = :addUser"),
    @NamedQuery(name = "Beers.findByLastMod", query = "SELECT b FROM Beers b WHERE b.lastMod = :lastMod"),
    @NamedQuery(name = "Beers.findByBuyPrice", query = "SELECT b FROM Beers b WHERE b.buyPrice = :buyPrice"),
    @NamedQuery(name = "Beers.findBySellPrice", query = "SELECT b FROM Beers b WHERE b.sellPrice = :sellPrice")})
public class Beers implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "brewery_id")
    private int breweryId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Column(name = "cat_id")
    private int catId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "style_id")
    private int styleId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "abv")
    private float abv;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ibu")
    private float ibu;
    @Basic(optional = false)
    @NotNull
    @Column(name = "srm")
    private float srm;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "add_user")
    private int addUser;
    @Basic(optional = false)
    @NotNull
    @Column(name = "last_mod")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastMod;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "image")
    private String image;
    @Basic(optional = false)
    @NotNull
    @Column(name = "buy_price")
    private double buyPrice;
    @Basic(optional = false)
    @NotNull
    @Column(name = "sell_price")
    private double sellPrice;

    @Column(table = "categories", name = "cat_name")
    private String catName;

    @Column(table = "styles", name = "style_name")
    private String styleName;

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

    public Beers() {
    }

    public Beers(Integer id) {
        this.id = id;
    }

    public Beers(Integer id, int breweryId, String name, int catId, int styleId, float abv, float ibu, float srm, String description, int addUser, Date lastMod, String image, double buyPrice, double sellPrice, String catName) {
        this.id = id;
        this.breweryId = breweryId;
        this.name = name;
        this.catId = catId;
        this.styleId = styleId;
        this.abv = abv;
        this.ibu = ibu;
        this.srm = srm;
        this.description = description;
        this.addUser = addUser;
        this.lastMod = lastMod;
        this.image = image;
        this.buyPrice = buyPrice;
        this.sellPrice = sellPrice;
          this.catName = catName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getBreweryId() {
        return breweryId;
    }

    public void setBreweryId(int breweryId) {
        this.breweryId = breweryId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public int getStyleId() {
        return styleId;
    }

    public void setStyleId(int styleId) {
        this.styleId = styleId;
    }

    public float getAbv() {
        return abv;
    }

    public void setAbv(float abv) {
        this.abv = abv;
    }

    public float getIbu() {
        return ibu;
    }

    public void setIbu(float ibu) {
        this.ibu = ibu;
    }

    public float getSrm() {
        return srm;
    }

    public void setSrm(float srm) {
        this.srm = srm;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getAddUser() {
        return addUser;
    }

    public void setAddUser(int addUser) {
        this.addUser = addUser;
    }

    public Date getLastMod() {
        return lastMod;
    }

    public void setLastMod(Date lastMod) {
        this.lastMod = lastMod;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public double getBuyPrice() {
        return buyPrice;
    }

    public void setBuyPrice(double buyPrice) {
        this.buyPrice = buyPrice;
    }

    public double getSellPrice() {
        return sellPrice;
    }

    public void setSellPrice(double sellPrice) {
        this.sellPrice = sellPrice;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Beers)) {
            return false;
        }
        Beers other = (Beers) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "brewery.src.model.Beers[ id=" + id + " ]";
    }

}

下面是我尝试使用@manytoone 注释

Beers.java


    @Entity
    @Table(name = "beers")


    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "Beers.findAll", query = "SELECT b FROM Beers b"),
        @NamedQuery(name = "Beers.findById", query = "SELECT b FROM Beers b WHERE b.id = :id"),
        @NamedQuery(name = "Beers.findByLikeName", query = "SELECT b FROM Beers b WHERE b.name LIKE CONCAT('%',:name,'%')"),
        @NamedQuery(name = "Beers.findByBreweryId", query = "SELECT b FROM Beers b WHERE b.breweryId = :breweryId"),
        @NamedQuery(name = "Beers.findByName", query = "SELECT b FROM Beers b WHERE b.name = :name"),
        @NamedQuery(name = "Beers.findByCatId", query = "SELECT b FROM Beers b WHERE b.catId = :catId"),
        @NamedQuery(name = "Beers.findByStyleId", query = "SELECT b FROM Beers b WHERE b.styleId = :styleId"),
        @NamedQuery(name = "Beers.findByAbv", query = "SELECT b FROM Beers b WHERE b.abv = :abv"),
        @NamedQuery(name = "Beers.findByIbu", query = "SELECT b FROM Beers b WHERE b.ibu = :ibu"),
        @NamedQuery(name = "Beers.findBySrm", query = "SELECT b FROM Beers b WHERE b.srm = :srm"),
        @NamedQuery(name = "Beers.findByAddUser", query = "SELECT b FROM Beers b WHERE b.addUser = :addUser"),
        @NamedQuery(name = "Beers.findByLastMod", query = "SELECT b FROM Beers b WHERE b.lastMod = :lastMod"),
        @NamedQuery(name = "Beers.findByBuyPrice", query = "SELECT b FROM Beers b WHERE b.buyPrice = :buyPrice"),
        @NamedQuery(name = "Beers.findBySellPrice", query = "SELECT b FROM Beers b WHERE b.sellPrice = :sellPrice")})
    public class Beers implements Serializable {
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="styleId")
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id")
        private Integer id;
        @Basic(optional = false)
        @NotNull
        @Column(name = "brewery_id")
        private int breweryId;
        @Basic(optional = false)
        @NotNull
        @Size(min = 1, max = 255)
        @Column(name = "name")
        private String name;
        @Basic(optional = false)
        @NotNull
        @Column(name = "cat_id")
        private int catId;
        @Basic(optional = false)
        @NotNull
        @Column(name = "style_id")
        private int styleId;
        @Basic(optional = false)
        @NotNull
        @Column(name = "abv")
        private float abv;
        @Basic(optional = false)
        @NotNull
        @Column(name = "ibu")
        private float ibu;
        @Basic(optional = false)
        @NotNull
        @Column(name = "srm")
        private float srm;
        @Basic(optional = false)
        @NotNull
        @Lob
        @Size(min = 1, max = 65535)
        @Column(name = "description")
        private String description;
        @Basic(optional = false)
        @NotNull
        @Column(name = "add_user")
        private int addUser;
        @Basic(optional = false)
        @NotNull
        @Column(name = "last_mod")
        @Temporal(TemporalType.TIMESTAMP)
        private Date lastMod;
        @Basic(optional = false)
        @NotNull
        @Lob
        @Size(min = 1, max = 65535)
        @Column(name = "image")
        private String image;
        @Basic(optional = false)
        @NotNull
        @Column(name = "buy_price")
        private double buyPrice;
        @Basic(optional = false)
        @NotNull
        @Column(name = "sell_price")
        private double sellPrice;

        @Column(table = "categories", name = "cat_name")
        private String catName;

        @Column(table = "styles", name = "style_name")
        private String styleName;


        public Beers() {
        }

        public Beers(Integer id) {
            this.id = id;
        }

        public Beers(Integer id, int breweryId, String name, int catId, int styleId, float abv, float ibu, float srm, String description, int addUser, Date lastMod, String image, double buyPrice, double sellPrice) {
            this.id = id;
            this.breweryId = breweryId;
            this.name = name;
            this.catId = catId;
            this.styleId = styleId;
            this.abv = abv;
            this.ibu = ibu;
            this.srm = srm;
            this.description = description;
            this.addUser = addUser;
            this.lastMod = lastMod;
            this.image = image;
            this.buyPrice = buyPrice;
            this.sellPrice = sellPrice;


        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public int getBreweryId() {
            return breweryId;
        }

        public void setBreweryId(int breweryId) {
            this.breweryId = breweryId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getCatId() {
            return catId;
        }

        public void setCatId(int catId) {
            this.catId = catId;
        }

        public int getStyleId() {
            return styleId;
        }

        public void setStyleId(int styleId) {
            this.styleId = styleId;
        }

        public float getAbv() {
            return abv;
        }

        public void setAbv(float abv) {
            this.abv = abv;
        }

        public float getIbu() {
            return ibu;
        }

        public void setIbu(float ibu) {
            this.ibu = ibu;
        }

        public float getSrm() {
            return srm;
        }

        public void setSrm(float srm) {
            this.srm = srm;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public int getAddUser() {
            return addUser;
        }

        public void setAddUser(int addUser) {
            this.addUser = addUser;
        }

        public Date getLastMod() {
            return lastMod;
        }

        public void setLastMod(Date lastMod) {
            this.lastMod = lastMod;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public double getBuyPrice() {
            return buyPrice;
        }

        public void setBuyPrice(double buyPrice) {
            this.buyPrice = buyPrice;
        }

        public double getSellPrice() {
            return sellPrice;
        }

        public void setSellPrice(double sellPrice) {
            this.sellPrice = sellPrice;
        }
           public String getCatName() {
            return catName;
        }

        public void setCatName(String catName) {
            this.catName = catName;
        }

        public String getStyleName() {
            return styleName;
        }

        public void setStyleName(String styleName) {
            this.styleName = styleName;
        }


        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Beers)) {
                return false;
            }
            Beers other = (Beers) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "brewery.src.model.Beers[ id=" + id + " ]";
        }

分类目录.java

@Entity
@Table(name = "categories")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Categories.findAll", query = "SELECT c FROM Categories c"),
    @NamedQuery(name = "Categories.findById", query = "SELECT c FROM Categories c WHERE c.id = :id"),
    @NamedQuery(name = "Categories.findByCatName", query = "SELECT c FROM Categories c WHERE c.catName = :catName"),
    @NamedQuery(name = "Categories.findByLastMod", query = "SELECT c FROM Categories c WHERE c.lastMod = :lastMod")})
public class Categories implements Serializable {
 @OneToMany(mappedBy = "beer")
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "cat_name")
    private String catName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "last_mod")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastMod;

    public Categories() {
    }

    public Categories(Integer id) {
        this.id = id;
    }

    public Categories(Integer id, String catName, Date lastMod) {
        this.id = id;
        this.catName = catName;
        this.lastMod = lastMod;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public Date getLastMod() {
        return lastMod;
    }

    public void setLastMod(Date lastMod) {
        this.lastMod = lastMod;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Categories)) {
            return false;
        }
        Categories other = (Categories) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "brewery.src.model.Categories[ id=" + id + " ]";
    }

}

样式.java

@Entity
@Table(name = "styles")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Styles.findAll", query = "SELECT s FROM Styles s"),
    @NamedQuery(name = "Styles.findById", query = "SELECT s FROM Styles s WHERE s.id = :id"),
    @NamedQuery(name = "Styles.findByCatId", query = "SELECT s FROM Styles s WHERE s.catId = :catId"),
    @NamedQuery(name = "Styles.findByStyleName", query = "SELECT s FROM Styles s WHERE s.styleName = :styleName"),
    @NamedQuery(name = "Styles.findByLastMod", query = "SELECT s FROM Styles s WHERE s.lastMod = :lastMod")})
public class Styles implements Serializable {
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="catId")
  @OneToMany(mappedBy = "beer")
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "cat_id")
    private int catId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "style_name")
    private String styleName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "last_mod")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastMod;

    public Styles() {
    }

    public Styles(Integer id) {
        this.id = id;
    }

    public Styles(Integer id, int catId, String styleName, Date lastMod) {
        this.id = id;
        this.catId = catId;
        this.styleName = styleName;
        this.lastMod = lastMod;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

    public Date getLastMod() {
        return lastMod;
    }

    public void setLastMod(Date lastMod) {
        this.lastMod = lastMod;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Styles)) {
            return false;
        }
        Styles other = (Styles) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "brewery.src.model.Styles[ id=" + id + " ]";
    }

}

500 错误根本原因

Exception
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1006)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.lang.ExceptionInInitializerError
    brewery.src.controller.beerService.getAllBeers(beerService.java:26)
    brewery.src.controller.beerController.getBeers(beerController.java:39)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Root Cause
javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [taste_PU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [categories] is not present in this descriptor.
Descriptor: RelationalDescriptor(brewery.src.model.Beers --> [DatabaseTable(beers)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(brewery.src.model.Beers --> [DatabaseTable(beers)])
4

0 回答 0