当我测试我的项目时,数据库中只创建了4个表,但没有创建其他表,我不知道为什么。创建了表通知
、位置
、dernierePotion
和user
,但没有创建表要求
和我没有放在本例中的其他表。有一些属性我忘记了?感谢您的帮助。以下是一些文件:
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>
拒绝位置.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>
通知.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
}
用户.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);
}
}
多对一和联接子类似乎存在问题。在冬眠论坛上对此进行了详细解释;我不完全清楚为什么会这样,但 sdknott 的解释(在该网站上)是:
您遇到的问题在于您的映射。通过声明多对一的约束列实际上是表 A 的主键列,Hibernate 将始终尝试解析实体 B,因为多对一列永远不会为空。
我已经使用了您的源代码,并将joined-subclass中的各种< code >多对一映射更改为< code >一对一映射,现在对我来说一切正常。希望这将允许您正确地表达您的模式。
(旁白)没有解决问题的原答案
我看到您在 Demande
类中有两个称为“用户”的成员;请记住,Demande
是 Notification
的子类并获取其成员。我从来没有尝试过,它看起来很奇怪,而且肯定会令人困惑。这可能是导致您的问题的原因。
如果你真的想要两个“用户”成员,给他们取更合适的名字(也许是“请求者”和“响应者”)?只是根据上下文猜测)。如果您只想要一个“用户”,那么您不需要将它放在< code>Demande中,它已经在< code>Notification中了。
问题内容: 在配置hibernate.cfg.xml中,我添加 Hibernate并在运行应用程序时自动创建表。但是,我通过运行drop table sql从数据库中手动删除该表。然后再次运行hibernate应用程序。出现异常 引起原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表’test.person’不存在 解决该问
我有一个第三方jar,可以容纳所有实体和映射。我目前正在经典的Spring-MVC应用程序中成功使用此jar,但现在我正在尝试在Spring-Boot应用程序中使用它。(1.5.7.发布) 我有这个用于我的应用程序.java: 由于它是第三方,我也必须进行会话扫描,所以我在我的@Configuration类中有这个: 在我的application.properties中: 我收到此错误: 现在表名
我使用的是Hibernate 4.3.6,我尝试通过将@Audited注释添加到我的一个@Entity类中来使用Envers功能。(envers jar-hibernate-envers-4.3.6.Final.jar-位于我的CLASSPATH上。) 当我运行没有@Audited注释的代码时,我得到一个org . hibernate . exception . sqlgrammarexcepti
问题内容: 我尝试在spring版本的项目中将hibernate模式从4升级到5 。升级之后,调用更新方法时,在堆栈跟踪中发现以下错误。 我用注解更改了自动递增的ID 错误仍然存在。 问题答案: 您需要设置Hibernate5.x ..参见并链接。 对于较早版本的hibernate 4.x:
我有一个关于电影和演员的数据库。我想做的是:我想从相应的数组列表中获取演员数据(如姓名、国家、出生日期),并将这些数据插入到我的演员表中。我想对我的电影桌做同样的事情。以下是我的部分代码: 然后我得到这些错误: > < li> 线程“main”javax . persistence . roll back异常:提交事务时出错 -原因:javax.persistence。PersistenceExc
我使用h2db进行测试,使用hibernate自动创建表,但希望flyway在数据库中插入脚本。所以,我的飞行路线没有创建脚本。 首先,我不确定这是否可能。但是当我使用一些插入脚本时,我的测试失败了,因为flyway无法插入记录,抱怨表格不存在。 似乎在h2创建表之前,它就已经在尝试运行脚本了。 我正在使用Spring靴来调用飞行路线。所以,我只需要这个依赖项和一个数据库。类路径中的migarat