映射@ManyToOne /外键Hibernate Java

时间:2018-01-03 02:11:27

标签: java mysql hibernate foreign-keys mapping

首先,对不起,如果主题已经被回答,但我已经连续搜索了近6个小时,并尝试了我在网上找到的所有内容,但我仍然卡住了。

我在执行代码时遇到问题。我无法找到它的来源,也无法找到解决方案......

这是我的不同课程:(顺便说一句,这是我在SOF上的第一篇文章,如果您需要更多信息,请告诉我。)

我将发布我的Pojo,有用的DAO和MySQL表格创建订单,以及我收到的错误消息。

的POJO:

-Celebrite:

@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
}

-Monument

@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
}

好了,现在DAO,我只发布了AssocieA的DAO,因为其他人都在完美地工作

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();
    }
}

最后,&#39;主要&#39;类

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");

- &GT;返回(&#34;成功&#34;)是因为我使用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]

我理解班级&#39; AssocieA&#39;没有&#34; numCelebrite&#34;属性,但我认为,由于@ManyToOne注释,应该在加载Associe时加载Celebrite表。

否则,你能给我一些解释如何做的提示吗? 最终目标是:拥有一个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);

对于那些想直接查看解决方案的人来说,它是OpenClassRoom上的法语

2 个答案:

答案 0 :(得分:0)

尝试删除名人pojo中的section .data space:db ' ' size :equ $-space num :db 4 no :db 4 section .text global _start _start: mov eax,3 ;read input number mov ebx,0 mov ecx,num mov edx,4 int 80h sub dword[num],30h ;convert from ASCII to integer again: ;incrementing the eax register value from 0(loop) mov eax,0 cmp eax,dword[num] ja finish mov dword[no],eax ;store eax value to no add eax,30h ;convert eax to ascii mov edx,4. ;write eax value mov ecx,eax mov ebx,1 mov eax,4 int 80h inc dword[no] ;incrementing no mov eax,dword[no] jmp again finish: ;exit mov eax,1 mov ebx,0 int 80h @JoinColumn(name="numCelebrite"),将@GeneratedValue(strategy=GenerationType.IDENTITY)更改为CascadeType.ALL并告诉我们发生了什么。我不是专家,但我认为你可以这样做。

答案 1 :(得分:0)

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具有属性“numCelebrite”

但在我的情况下,我将表映射到我的类:AssocieA表的每一行现在都是我的AssocieA类的一个实例。 使用HQL查询,我正在请求我的类的实例,而不是我的数据库的表。 所以我要做的是a.getCelebrite()。getNumCelebrite(),因此“a.celebrite.numCelebrite”

我希望自己足够清楚,因为我的英语不太好。

这是论坛的链接我有这个答案(用法语): https://openclassrooms.com/forum/sujet/org-hibernate-queryexception?page=1#message-92165885