mapping@manytone/foreign key hibernate java

hwamh0ep  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(350)

首先,如果这个问题已经被回答了,我很抱歉,但是我已经连续搜索了将近6个小时,并且尝试了所有我能在网上找到的东西,但是我仍然被卡住了。。
我在执行代码时遇到问题。。我找不到它的起源,也找不到解决办法。。。
以下是我的不同课程:(顺便说一句,这是我在软件上的第一篇文章,如果你需要更多信息,请告诉我)。
我将发布我的pojo、有用的dao和mysql表创建命令,以及我得到的错误消息。
波乔:
-西乐葆:

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

private static final long serialVersionUID = 1L;

@Id
@Column(name="numCelebrite")
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JoinColumn(name="numCelebrite")
private int numCelebrite;

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

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

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

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

public Celebrite() {
    super();
}

public Celebrite(String nom, String prenom, String nationalite, String epoque) {
    super();
    this.nom = nom;
    this.prenom = prenom;
    this.nationalite = nationalite;
    this.epoque = epoque;
}

public int getNumCelebrite() {
    return numCelebrite;
}

public void setNumCelebrite(int numCelebrite) {
    this.numCelebrite = numCelebrite;
}
//Other getters/setters
}

-纪念碑

@Entity
public class Monument implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String codeM;
    private String nomM;
    private String proprietaire;
    private String typeMonument;
    private float latitude;
    private float longitude;

    public Monument() {
        super();
    }
    public Monument( String codeM,String nomM, String propritaire, String typeMonument, float latitude, float longitude) {
        super();
        this.codeM=codeM;
        this.nomM = nomM;
        this.proprietaire = propritaire;
        this.typeMonument = typeMonument;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public Monument( String nomM, String propritaire, String typeMonument, float latitude, float longitude) {
        super();

        this.nomM = nomM;
        this.proprietaire = propritaire;
        this.typeMonument = typeMonument;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getCodeM() {
        return codeM;
    }

    public void setCodeM(String codeM) {
        this.codeM = codeM;
    }

    //other getters/setters..
}

-associea(翻译:associatedto)

@Entity
public class AssocieA  implements Serializable{
    private static final long serialVersionUID = 1L;
     @Id
     @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
     @JoinColumn(name="numCelebrite",referencedColumnName="numCelebrite")
     private Celebrite celebrite;
     //private int numCelebrite;

     @Id
     @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
     @JoinColumn(name="codeM",referencedColumnName="codeM")
    //private String codeM;
     private Monument monument;

    public AssocieA() {
    }

        public AssocieA(Celebrite celebrite, Monument monument) {
        super();
        this.celebrite = celebrite;
        this.monument = monument;
    }

//getters/setters
}

好了,现在道具,我只发布了阿索西亚的道具,因为其他道具都工作得很好

public class DAOAssocieA {
    EntityManagerFactory emf;
    EntityManager em ;

    public DAOAssocieA() {
        super();
        this.emf = Persistence.createEntityManagerFactory("jpa-societe-pu");
        this.em = emf.createEntityManager();
    }

    public List<AssocieA> getAssociatedMonuments(int numCelebrite){
        Query req=em.createQuery("Select a from AssocieA a where a.numCelebrite =" + numCelebrite);
        return  req.getResultList();
    }

    public List<AssocieA> getAssociatedCelebrities(String codeM){
        Query req=em.createQuery("Select a from AssocieA a where a.codeM = '"+codeM+"'");
        return req.getResultList();
    }
}

最后,“主要”类

public String execute() {
        setDAOc(new DAOCelebrite());
        setDAOm(new DAOMonument());
        setDAOa(new DAOAssocieA());
        if (getNom()==null)
            setNom("");
        if (getPrenom() == null)
            setPrenom("");
        if (getNationalite() == null)
            setNationalite("");
        if (getEpoque()==null)
            setEpoque(""); 
        setListeCelebrite(DAOc.getCelebritiesBy(getNom(),getPrenom(),getNationalite(), getEpoque()));
        System.out.println(getAssociated());
        if (getAssociated().equals("on")) {
            for (Celebrite c:listeCelebrite) {
                for (AssocieA a : DAOa.getAssociatedMonuments(c.getNumCelebrite())){
                    System.out.println(a.getCelebrite());
                    System.out.println(a.getMonument());
                }  
            }
        }  

        return ("success");

->回报(“成功”)是因为我使用struts2
现在,表创建顺序(在mysql上)

CREATE TABLE Celebrite (numCelebrite int auto_increment, nom varchar(16), prenom varchar(16), nationalite varchar(10), epoque varchar(6), PRIMARY KEY (numCelebrite)) ENGINE=InnoDB;

CREATE TABLE Monument (codeM varchar(5), nomM varchar(25), proprietaire varchar(10), typeMonument varchar(16), longitude float, latitude float, PRIMARY KEY (codeM)) ENGINE=InnoDB;

CREATE TABLE AssocieA (codeM varchar(5), numCelebrite int, PRIMARY KEY (codeM,numCelebrite), FOREIGN KEY (codeM) REFERENCES Monument(codeM), FOREIGN KEY (numCelebrite) REFERENCES Celebrite(numCelebrite)) ENGINE=InnoDB;

最后,我得到一条错误消息:

org.hibernate.QueryException: could not resolve property: numCelebrite of: pojo.AssocieA [Select a from pojo.AssocieA a where a.numCelebrite =1]

我知道类'associea'没有“numcelebrite”属性,但是我认为由于@manytone注解的缘故,应该在加载associe时加载celerite表。
另外,你能给我一些提示来解释怎么做吗?最后的目标是:有一个celebrite,我想,使用numcelebrite,检索每一个相关的纪念碑,使用associea表。
先谢谢你
编辑:kulturman在另一个网站上找到的解决方案:我使用的是本地查询:

em.createQuery("from AssocieA a where a.numCelebrite  =" + numCelebrite);

代替jpql(hql查询):

em.createQuery("from AssocieA a where a.celebrite.numCelebrite  =" + numCelebrite);

对于那些想直接看到解决方案的人来说,它是用法语在open教室上发布的

tzxcd3kk

tzxcd3kk1#

尝试删除 @JoinColumn(name="numCelebrite") 以及 @GeneratedValue(strategy=GenerationType.IDENTITY) 在名人pojo,改变 CascadeType.ALLCascadeType.Persist 告诉我们会发生什么。我不是Maven,但我认为这样你就能做到。

42fyovps

42fyovps2#

kulturman在另一个网站上找到的解决方案:
我必须替换:

em.createQuery("from AssocieA a where a.numCelebrite  =" + numCelebrite);
Instead of JPQL (HQL queries):

签署人:

em.createQuery("from AssocieA a where a.celebrite.numCelebrite  =" + numCelebrite);

解释如下:
我在构建查询时就像在请求数据库一样:表associea具有属性“numelebrite”
但是在我的例子中,我将表Map到我的类:associea表的每一行现在都是associea类的一个示例。使用hql查询,我请求的是类的示例,而不是数据库的表。所以我要做的是a.getcelerite().getnumcelebrite(),因此是“a.celerite.numcelebrite”
我希望我已经说清楚了,因为我的英语不太好。
以下是我得到这个答案的论坛链接(法语):https://openclassrooms.com/forum/sujet/org-hibernate-queryexception?page=1#message-92165885

相关问题