Hibernate表不存在

时间:2014-02-16 09:47:56

标签: database hibernate hibernate-mapping

当我测试我的项目时,只在数据库中创建了4个表而不是其他表,我不知道为什么。已创建表notificationpositiondernierePositionuser,但未创建表demande以及我未在此示例中添加的其他表。我忘记了一些属性? 谢谢你的帮助。 以下是一些文件:

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 name="">
  <!-- Database connection settings, Connect to HSQL, IN Memory  -->
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/***</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.connection.username">***</property>
  <property name="hibernate.connection.password"/>
  <!-- DB schema will be updated if needed -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- JDBC connection pool (use the built-in) 
 List of XML mapping files -->
  <mapping class="modele.Demande" resource="Demande.hbm.xml"/>
  <mapping class="modele.DernierePosition" resource="DernierePosition.hbm.xml"/>
  <mapping class="modele.Group" resource="Group.hbm.xml"/>
  <mapping class="modele.Invitation" resource="Invitation.hbm.xml"/>
  <mapping class="modele.Marqueur" resource="Marqueur.hbm.xml"/>
  <mapping class="modele.Notification" resource="Notification.hbm.xml"/>
  <mapping class="modele.NotificationMarqueur" resource="NotificationMarqueur.hbm.xml"/>
  <mapping class="modele.Position" resource="Position.hbm.xml"/>
  <mapping class="modele.User" resource="User.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Position.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="modele.Position" table="POSITION">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="lattitude" type="double">
            <column name="LATTITUDE" />
        </property>
        <property name="longitude" type="double">
            <column name="LONGITUDE" />
        </property>
    </class>
</hibernate-mapping>

DenierePosition.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <joined-subclass name="modele.DernierePosition" extends="modele.Position" table="DERNIEREPOSITION" lazy="false">
        <key>
            <column name="ID" />
        </key>
        <property name="time" type="java.util.Date">
            <column name="TIME" />
        </property>
        <one-to-one name="user" class="modele.User"></one-to-one>
    </joined-subclass>
</hibernate-mapping>

Notification.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="modele.Notification" table="NOTIFICATION">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <many-to-one name="user" class="modele.User" fetch="join">
            <column name="USER" />
        </many-to-one>
    </class>
</hibernate-mapping>

Demande.java

@XmlRootElement
public class Demande extends Notification {

    private Group group;

    private User demandeur;

    public Demande(){

    }

    public Demande(Group group, User demandeur) {
        super(group.getProprietaire());
        this.group = group;
        this.demandeur=demandeur;
    }

    // Getters and setters


}

Demande.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <joined-subclass name="modele.Demande" extends="modele.Notification" table="DEMANDE" lazy="false">
        <key>
            <column name="ID" />
        </key>
        <many-to-one name="group" class="modele.Group" fetch="join">
            <column name="GROUP" />
        </many-to-one>
        <many-to-one name="demandeur" class="modele.User" fetch="join">
            <column name="DEMANDEUR" />
        </many-to-one>
    </joined-subclass>
</hibernate-mapping>

User.java

@XmlRootElement
public class User {

    /** Attributs */ 

    private int id;

    private String pseudo;

    private String telephone;

    private String email;

    @XmlTransient
    private Set<Demande> demandes;

    @XmlTransient
    private Set<Invitation> aInvite;

    @XmlTransient
    private Set<Notification> notifications;

    private DernierePosition dernierePosition;

    @XmlTransient
    private Set<Group> groups;

    @XmlTransient
    private Set<Group> proprietaire;

    /** Constructeur */ 
    public User() {
    }

    public User(String telephone, String pseudo, String email) {
        super();
        this.pseudo = pseudo;
        this.telephone = telephone;
        this.email = email;
    }
// Getters and setters
}

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="modele.User" table="USER">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="pseudo" type="java.lang.String">
            <column name="PSEUDO" />
        </property>
        <property name="telephone" type="java.lang.String" not-null="true" unique="true">
            <column name="TELEPHONE" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <set name="demandes" table="DEMANDE" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Demande" />
        </set>
        <set name="aInvite" table="INVITATION" inverse="false" lazy="true" access="field">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Invitation" />
        </set>
        <set name="notifications" table="NOTIFICATION" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Notification" />
        </set>
        <one-to-one name="dernierePosition" class="modele.DernierePosition"></one-to-one>
        <set name="groups" table="GROUP" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Group" />
        </set>
        <set name="proprietaire" table="GROUP" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Group" />
        </set>
    </class>
</hibernate-mapping>

Group.java

@XmlRootElement
public class Group {

    /** Attributs */

    private int id;

    private String description;

    private String hashtag;

    private String password;

    private Set<Marqueur> marqueurs;

    @XmlTransient
    private Set<Invitation> invitations;

    @XmlTransient
    private Set<Demande> demandes;

    private User proprietaire;

    private Set<User> invites;

    /** 
     * Constructeur 
     * */ 
    public Group()
    {

    }

    public Group(String description, String hashtag, User proprietaire, String password) {
        super();
        this.description = description;
        this.hashtag = hashtag;
        this.proprietaire = proprietaire;
        this.setPassword(password);
    }

Group.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16 f?vr. 2014 11:30:30 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="modele.Group" table="GROUP">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="description" type="java.lang.String">
            <column name="DESCRIPTION" />
        </property>
        <property name="hashtag" type="java.lang.String">
            <column name="HASHTAG" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <set name="marqueurs" table="MARQUEUR" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Marqueur" />
        </set>
        <set name="invitations" table="INVITATION" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.Invitation" />
        </set>
        <many-to-one name="proprietaire" class="modele.User" fetch="join">
            <column name="PROPRIETAIRE" />
        </many-to-one>
        <set name="invites" table="USER" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="modele.User" />
        </set>
    </class>
</hibernate-mapping>

测试用户

package dao;

import java.util.List;

import junit.framework.Assert;
import modele.User;

import org.junit.Test;


public class UserDaoTest {

    private final String tel = "0601020304";
    private final String pseudo = "pseudo";
    private final String pseudoModified = "pseudo2";
    private final String email = "email@email.com";

    @Test
    public void addUserTest(){

        User initialUser = new User(tel, pseudo, email);

        UserDao.addUser(initialUser);

        User addedUser = UserDao.getUser(tel);

        Assert.assertEquals(addedUser.getTelephone(), initialUser.getTelephone());
        Assert.assertEquals(addedUser.getPseudo(), initialUser.getPseudo());
        Assert.assertEquals(addedUser.getEmail(), initialUser.getEmail());
    }

    @Test
    public void modifyUserTest(){
        UserDao.modifyUser(tel, pseudoModified, email);

        User modifiedUser = UserDao.getUser(tel);

        Assert.assertEquals(modifiedUser.getTelephone(), tel);
        Assert.assertEquals(modifiedUser.getPseudo(), pseudoModified);
        Assert.assertEquals(modifiedUser.getEmail(), email);

    }

    @Test
    public void listUserTest(){
        User modifiedUser = UserDao.getUser(tel);

        List<User> listUsers = UserDao.listUser();

        User listedUser = listUsers.get(listUsers.indexOf(modifiedUser));

        Assert.assertEquals(listedUser.getTelephone(), modifiedUser.getTelephone());
        Assert.assertEquals(listedUser.getPseudo(), modifiedUser.getPseudo());
        Assert.assertEquals(listedUser.getEmail(), modifiedUser.getEmail());
    }

    @Test
    public void deleteUserTest(){
        User user = UserDao.getUser(tel);

        UserDao.deleteUser(user.getTelephone());

        List<User> listUsers = UserDao.listUser();

        Assert.assertEquals(listUsers.indexOf(user), -1);
    }
}

1 个答案:

答案 0 :(得分:2)

似乎多对一和连接子类存在问题。关于the hibernate forums的详细解释;我不清楚为什么会这样,但sdknott(在那个网站上)的解释是:

  

您遇到的问题是您的映射。通过声明多对一的约束列实际上是表A的主键列,Hibernate将始终尝试解析实体B,因为多对一列永远不会为空。

我已经使用了你的源代码并将连接子类中的各种many-to-one映射更改为one-to-one映射,现在它对我有用。希望这将允许您正确表达您的架构。


(旁白)未解决问题的原始答案

我看到你在Demande类中有两个名为“user”的成员;请记住DemandeNotification的子类并获取其成员。我从来没有尝试过,它看起来很奇怪,而且肯定会让人感到困惑。这可能是造成问题的原因。

如果你真的想要两个“用户”成员,给他们更合适的名字(可能是“请求者”和“响应者”?只是从上下文猜测)。如果您只想要一个“用户”,那么您无需将其放在Demande中,它已经在Notification中。