我想用hibernate 3创建一个动态Web项目。在这个项目中我有2个类和它们的关系。第一节是 Ecole ,这是attributs
package essai1;
import java.util.HashSet; import java.util.Set;
公共课程Ecole {
private int id_ecole;
private String nom_ecole;
private Set<Etudiant> etudiants = new HashSet<>();
public Set<Etudiant> getEtudiants() {
return etudiants;
}
public void setEtudiants(Set<Etudiant> etudiants) {
this.etudiants = etudiants;
}
public int getId_ecole() {
return id_ecole;
}
public void setId_ecole(int id_ecole) {
this.id_ecole = id_ecole;
}
public String getNom_ecole() {
return nom_ecole;
}
public void setNom_ecole(String nom_ecole) {
this.nom_ecole = nom_ecole;
}
public Ecole() {
super();
}}
第二类有此属性
package essai1;
public class Etudiant {
private int id_etudiant;
private String nom_etudiant;
private String prenom_etudiant;
private Ecole ecole;
public int getId_etudiant() {
return id_etudiant;
}
public void setId_etudiant(int id_etudiant) {
this.id_etudiant = id_etudiant;
}
public String getNom_etudiant() {
return nom_etudiant;
}
public void setNom_etudiant(String nom_etudiant) {
this.nom_etudiant = nom_etudiant;
}
public String getPrenom_etudiant() {
return prenom_etudiant;
}
public void setPrenom_etudiant(String prenom_etudiant) {
this.prenom_etudiant = prenom_etudiant;
}
public Ecole getEcole() {
return ecole;
}
public void setEcole(Ecole ecole) {
this.ecole = ecole;
}
public Etudiant() {
super();
}}
我有一个映射Ecole.hbm.xml的文件夹,通过它我可以创建这个文件
<?xml version="1.0" encoding='UTF-8' ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="essai1.Ecole" table="ECOLE">
<id name="id_ecole" type="java.lang.Integer" column="id_ecole">
<generator class="increment" />
</id>
<property name="nom_ecole" type="java.lang.String">
<column name="nom_ecole" />
</property>
<set name="etudiants" inverse="true" table="ETUDIANT" cascade="all">
<key>
<column name="id_ecole" not-null="true"></column>
</key>
<one-to-many class="essai1.Etudiant" column="idEtudiant" />
</set>
<!-- <bag name="etudiants" table="Etudiant" lazy="true" inverse="true"
cascade="all"> <key column="id_ecole" not-null="true" /> <one-to-many class="essai1.Etudiant">
<column name="idEtudint" ></column> </one-to-many> </bag> -->
</class>
</hibernate-mapping>
在这个文件中,我遇到了Ecole类和ligne 一对多中的Etudiant类之间的映射问题,我无法添加属性column =“idEtudiant “在这个标签中 hibernate.cfg.xml的文件在这里
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/essaihibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="map/Ecole.hbm.xml" />
<mapping resource="map/Etudiant.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Etudiant.hbm.xml的映射文件清楚,没有问题,这是代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="essai1.Etudiant" table="Etudiant">
<id name="idEtudiant" type="java.lang.Integer" column="idEtudiant">
<generator class="increment" />
</id>
<property name="nomEtudiant" type="java.lang.String">
<column name="nomEtudiant" />
</property>
<property name="prenomEtudiant" type="java.lang.String">
<column name="prenomEtudiant" />
</property>
<many-to-one name="essai1.ecole">
<column name="id_ecole" />
</many-to-one>
</class>
</hibernate-mapping>
hibernateUtil.java的文件也在那里
package util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Création de la SessionFactory à partir de hibernate.cfg.xml
sessionFactory = new Configuration().configure("map/hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
// public static final ThreadLocal session = new ThreadLocal();
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
罐子下载了:
log4j.jar
MySQL的连接器的Java-5.0.4-bin.jar
数据库
create database essaihibernate; use essaihibernate; CREATE TABLE `essaihibernate`.`ECOLE` ( `id_ecole` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `nom_ecole` TEXT NOT NULL, PRIMARY KEY (`id_ecole`) );
和第二个表Etudiant在那里
CREATE TABLE `essaihibernate`.`ETUDIANT` ( `id_etudiant` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `nom_etudiant` TEXT NOT NULL, `prenom_etudiant` TEXT NOT NULL, `id_ecole` INTEGER UNSIGNED NOT NULL, PRIMARY KEY (`id_etudiant`), CONSTRAINT `FK_ETUDIANT_1` FOREIGN KEY `FK_ETUDIANT_1` (`id_ecole`) REFERENCES `ecole` (`id_ecole`) ON DELETE RESTRICT ON UPDATE RESTRICT );
该文件强制执行此所有指令的命令,但我可以映射到我的数据库,因为我在ligne 17的文件Ecole.hbm.xml中的列有问题
<one-to-many class="essai1.Etudiant" column="idEtudiant" />
错误属性“列”必须为元素类型“一对多”声明 我能做什么?我非常失望。帮助我,我的朋友,我在等你
答案 0 :(得分:0)
这是来自Etudiant类
private int id_etudiant;
private String nom_etudiant;
private String prenom_etudiant;
不是您在相应的xml文件Etudiant.hbm.xml
中定义的内容