首先,很抱歉,如果您已经回答了这个问题,但是我已经连续搜索了将近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
}


-纪念碑

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


最后,“主”课

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”属性,但是我认为由于@ManyToOne批注,应在加载Associe时加载Celebrite Table。

否则,您能给我一些提示来解释如何做吗?
最终目标是:我希望使用numCelebrite来创建一个Celebrite,并使用AssocieA表检索与其相关的所有纪念碑。

先感谢您

编辑:kulturman在另一个网站上找到的解决方案:
我正在使用本机查询:

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


代替JPQL(HQL查询):

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


对于那些想直接看解决方案的人,它在OpenClassRoom中是法语的

最佳答案

尝试删除名人pojo中的@JoinColumn(name="numCelebrite")@GeneratedValue(strategy=GenerationType.IDENTITY),将CascadeType.ALL更改为CascadeType.Persist,让我们知道会发生什么。我不是专家,但我认为您可以做到这一点。

07-26 06:22