org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性。发生错误:java.lang.NullPointerException

时间:2018-10-22 19:31:10

标签: java mysql hibernate orm

我正在与hibernate合作,尝试与SQL进行映射。

每次我尝试运行代码时,都会收到错误消息:

org.hibernate.AnnotationException: mappedBy reference an unknown    
target entity property: Catalog.primaryKey.suppliers in 
Suppliers.catalogSet There is an error:java.lang.NullPointerException

我已经进行了三次检查,以确保正确映射了所有内容,但是我必须丢失某些内容。有谁知道为什么我的给定代码会得到一个空指针?这些是我的班级代码:

导入javax.persistence。*;

@Entity
@Table(name = "catalog")
@AssociationOverrides({
@AssociationOverride(name = "primaryKey.suppliers",
        joinColumns = @JoinColumn(name = "sid")),
@AssociationOverride(name = "primaryKey.parts",
        joinColumns = @JoinColumn(name = "pid"))
})
public class Catalog {

//Composite ID
private CatalogId primaryKey = new CatalogId();

//Additional Column
private String cost;

@EmbeddedId
public CatalogId getPrimaryKey() {
    return primaryKey;
}

public void setPrimaryKey(CatalogId primaryKey) {
    this.primaryKey = primaryKey;
}

@Transient
public Suppliers getSuppliers(){
    return getPrimaryKey().getSupplier();
}

public void setSuppliers(Suppliers supplier){
    getPrimaryKey().setSupplier(supplier);
}

@Transient
public Parts getParts(){
    return getPrimaryKey().getParts();
}
public void setParts(Parts part){
    getPrimaryKey().setParts(part);
}


@Column(name = "cost")
public String getCost() {
    return cost;
}
public void setCost(String cost) {
    this.cost = cost;
}
}

import java.io.Serializable;

import javax.persistence.*;

@Embeddable
public class CatalogId implements Serializable{
private Suppliers suppliers;
private Parts parts;

@ManyToOne (cascade = CascadeType.ALL)
public Suppliers getSupplier() {
    return suppliers;
}
public void setSupplier(Suppliers suppliers) {
    this.suppliers = suppliers;
}

@ManyToOne(cascade = CascadeType.ALL)
public Parts getParts() {
    return parts;
}
public void setParts(Parts parts) {
    this.parts = parts;
}
}



 import java.util.*;
 import javax.persistence.*;

 @Entity
 @Table(name = "suppliers")
 public class Suppliers {

private long sid;
private String sname;
private String address;

private Set<Catalog> catalogSet = new HashSet<Catalog>();

public Suppliers(String sname, String address) {
    this.sname = sname;
    this.address = address;
}

public Suppliers() {

}

@Id
@GeneratedValue
@Column(name = "sid")
public long getSid() {
    return this.sid;
}

public void setSid(long sid) {
    this.sid = sid;
}

@Column(name= "sname")
public String getSname() {
    return this.sname;
}

public void setSname(String sname) {
    this.sname = sname;
}

@Column(name = "address")
public String getAddress() {
    return this.address;
}

public void setAddress(String address) {
    this.address = address;
}

@OneToMany(mappedBy = "primaryKey.suppliers", cascade = CascadeType.ALL)
public Set<Catalog> getCatalogSet() {
    return catalogSet;
}

public void setCatalogSet(Set<Catalog> catalogSet) {
    this.catalogSet = catalogSet;
}

public void addRecipe(Catalog catalog){
    this.catalogSet.add(catalog);
}
}

import java.util.*;
import javax.persistence.*;

@Entity
@Table(name = "parts")
public class Parts {
private long pid;
private String pname;
private String color;
private Set<Catalog> catalogSet = new HashSet<Catalog>();

public Parts(String pname, String color) {
    super();
    this.pname = pname;
    this.color = color;
}

public Parts() {
    super();
}

@Id
@GeneratedValue
@Column(name = "pid")
public long getPid() {
    return pid;
}

public void setPid(long pid) {
    this.pid = pid;
}

@Column(name = "pname")
public String getPname() {
    return pname;
}

public void setPname(String pname) {
    this.pname = pname;
}

@Column(name = "color")
public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

@OneToMany(mappedBy = "primaryKey.parts", cascade = CascadeType.ALL)
public Set<Catalog> getCatalogSet() {
    return this.catalogSet;
}

public void setCatalogSet(Set<Catalog> catalogSet) {
    this.catalogSet = catalogSet;
}

public void addCatalog(Catalog catalog){
    this.catalogSet.add(catalog);
}
}

这是我的(未完成的查询代码):

 import org.hibernate.*;
 import org.hibernate.boot.*;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import javax.swing.*;

 public class HibernateHql 
 {
public static void main(String[] args) 
{
    HibernateSession hibernateSession = new HibernateSession();
    SessionFactory sessionFactory;
    Session session;
    String option = "";
    String instruction = "Enter a: Return distinct sids and snames of suppliers who supply a red part or a green part." + "\n" +
                         "Enter b: Return distinct pids, pnames, and the maximum cost of that part amoung all suppliers that offer the part and that maximum cost is less than 70." + "\n" +
                         "Enter c: Insert a new catalog row given the supplier id, the part id, and the cost fromthe user of your program." + "\n" +
                         "Enter d: Quit Program";
    try
    {
        while(true)
        {
            option = JOptionPane.showInputDialog(instruction);

            hibernateSession.setUp();
            sessionFactory = hibernateSession.getSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();

            if(option.equals("a"))
            {
                List results = session.createQuery("select distinct c from Parts p join p.catalogSet c where p.color = :color1 or p.color = :color2").setParameter("color1", "red").setParameter("color2", "green").getResultList();
                String toShow = "These are suppliers who supply a red or green part:\n";
                for(int i=0;i<results.size();i++)
                {
                    Suppliers theSupplier = (Suppliers)((Catalog)results.get(i)).getSuppliers();
                    toShow += (i+1) + ") " + theSupplier.getSname() + "\n";
                }
                JOptionPane.showMessageDialog(null, toShow);
            }
            else if(option.equals("b"))
            {
                List results = session.createQuery("select max(r.cost)from Suppliers s inner join s.catalogSet r group by f").getResultList();
                String toShow = "These are all parts:\n";
                for(int i=0;i<results.size();i++)
                {
                    Parts thePart = (Parts)((Catalog)results.get(i)).getParts();
                    String cost = ((Catalog)results.get(i)).getCost();
                    if(Integer.parseInt(cost) <= 70){
                        toShow += (i+1) + ") " + thePart.getPname() + " " + cost;
                    }
                }
                JOptionPane.showMessageDialog(null, toShow);
            }
            else if(option.equals("c"))
            {

                String allSuppliers = "SID" + "....." + "SNAME" + "\n";
                List allS = session.createQuery("from Suppliers").getResultList();
                for(int i=0;i<allS.size();i++)
                {
                    Suppliers theSupplier = (Suppliers)allS.get(i);
                    allSuppliers += theSupplier.getSid() + "....." + theSupplier.getSname() + "\n";
                }

                JOptionPane.showMessageDialog(null, allSuppliers);

                String supplierID = JOptionPane.showInputDialog("Enter Supplier ID: ");

                String allParts = "PID" + "....." + "PNAME" + "\n";
                List allP = session.createQuery("from Parts p where p.color = :color").setParameter("color", "blue").getResultList();
                for(int i=0;i<allS.size();i++)
                {
                    Parts theParts = (Parts)allP.get(i);
                    allParts += theParts.getPid() + "....." + theParts.getPname() + "\n";
                }

                JOptionPane.showMessageDialog(null, allParts);

                String partsID = JOptionPane.showInputDialog("Enter Parts ID: ");




 //                 List results = session.createQuery("select f1 from Food f1 where f1 not in (select distinct r.primaryKey.food from Ingredient i inner join i.recipeSet r where i.iname = :iname)").setParameter("iname", "Green Onion").getResultList();
 //                 String toShow = "These foods don't have green onion:\n";
 //                 for(int i=0;i<results.size();i++)
 //                 {
 //                     Food theFood = (Food)results.get(i);
 //                     toShow += (i+1) + ") " + theFood.getFname() + "\n";
 //                 }
 //                 JOptionPane.showMessageDialog(null, toShow);
            }
            else
            {
                break;
            }

            session.getTransaction().commit();
            session.close();
        }
        JOptionPane.showMessageDialog(null, "Good Bye");
    }
    catch(Exception e)
    {
        System.out.println("There is an error:");
        System.out.println(e.toString());
    }
}

}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Suppliers类中,您已如下映射catalogSet

@OneToMany(mappedBy = "primaryKey.suppliers", cascade = CascadeType.ALL)
public Set<Catalog> getCatalogSet() {
  return catalogSet;
}

我在Suppliers类中没有找到任何名为primaryKey的字段。我认为您应该在此处使用sid来映射catalogSetSuppliers类之间的关系。

如果我误解了你的问题,请告诉我。