0

各位好日子:

请看我的 xml 映射:

User.hbm.xml

尤其是看inverse="false"<index column="EVENT_INDEX" />

<hibernate-mapping package="demidov.pkg.domain">
    <class name="TheUser" table="USER">
        <id name="userId" column="ID" type="integer">
            <generator class="native"/>         
        </id>

        <property name="userFirstName" column="FIRST_NAME" type="string" not-null="true"/>
        <property name="userLastName" column="LAST_NAME" type="string"/>
        <property name="userEmail" column="EMAIL" type="string" not-null="true"/>
        <property name="personAddress" column="PERSON_ADDRESS" type="string" />
        <property name="personPhoneNumber" column="PERSON_PHONE" type="string" />
        <property name="userGender" column="GENDER" type="string"/>

        <list name="userEventsList" inverse="false" lazy="true" fetch="select" cascade="all" >
            <key column="USER_ID" not-null="true" />
            <index column="EVENT_INDEX" />          
            <one-to-many class="UserEvents"/>       
        </list>
    </class>
</hibernate-mapping>

UserEvents.hbm.xml

特别看 <many-to-one name="theUser" class="TheUser" insert="false" update="false" lazy="false" fetch="select" cascade="save-update">

<hibernate-mapping package="demidov.pkg.domain">
    <class name="UserEvents" table="USER_EVENTS">
        <id name="userEventId" column="ID" type="integer">
            <generator class="native"/>
        </id>
        <property name="userEventDate" column="EVENT_DATE" type="date"/>
        <many-to-one name="theUser" class="TheUser" insert="false" update="false" lazy="false" fetch="select" cascade="save-update">
            <column name="USER_ID" not-null="true"/>
        </many-to-one>

        <joined-subclass name="ComputerMaintenanceEvent" table="MAINTENANCE_EVENT">
        <key column="MAINTENANCE_ID" not-null="true"/>
        <property name="descriptionMaintenance" column="DESCRIPTION_MAINTENANCE" type="string"/>
            <property name="additionalInfoMaintenance" column="ADDITIONAL_INFO_MAINTENANCE" type="string"/>
        </joined-subclass>              

        <joined-subclass name="SoftwareDevelopmentEvent" table="DEVELOPMENT_EVENT">
        <key column="DEVELOPMENT_ID" not-null="true"/>
        <property name="descriptionDevelopment" column="DESCRIPTION_DEVELOPMENT" type="string"/>
        <property name="additionalInfoDevelopment" column="ADDITIONAL_INFO_DEVELOPMENT" type="string"/>
        </joined-subclass>
    </class>
</hibernate-mapping>

因此,因为在我User.hbm.xml的 isinverse="false"和我UserEvents.hbm.xml的 isinsert="false" update="false"当我想将用户 + userevents 保存到我的数据库时,我<index column="EVENT_INDEX/>将得到更新,并且我将对集合中的元素(0、1、2 等)进行索引计数。像这样:

+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+
| ID | EVENT_DATE | USER_ID | EVENT_INDEX | MAINTENANCE_ID | DESCRIPTION_MAINTENANCE | ADDITIONAL_INFO_MAINTENANCE |
+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+
|  1 | 2013-10-31 |       1 |           0 |              2 | Description maitnance   |  Maitnance Test             |
|  2 | 2013-10-31 |       1 |           1 |              2 | Description maitnance   |  Maitnance Test             |
+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+

但是当我在我User.hbm.xml的 isinverse="true"和我UserEvents.hbm.xml的 is 中insert="true" update="true"并且想要将 user + userevents 保存到我的数据库时,我<index column="EVENT_INDEX/>在数据库中将具有null插入集合的每个元素的值。像这样:

+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+
| ID | EVENT_DATE | USER_ID | EVENT_INDEX | DEVELOPMENT_ID | DESCRIPTION_DEVELOPMENT | ADDITIONAL_INFO_DEVELOPMENT |
+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+
|  1 | 2013-10-31 |       1 |        NULL |              1 | Develop                 | Development Test            |
|  2 | 2013-10-31 |       1 |        NULL |              1 | Develop                 | Development Test            |
+----+------------+---------+-------------+----------------+-------------------------+-----------------------------+

另一个问题是为什么我的休眠插入复制相同的值多次包含我的集合元素。正如您从表中看到的那样,我正在尝试将一个 DESCRIPTION_DEVELOPMENT 和一个 DESCRIPTION_MAINTENANCE 对象保存到 DB -> 它们在一个集合中,但是休眠将它们保存两次,因为我在同一个集合中有 2 个但不同的元素。

我如何插入:

List<UserEvents> eventList  = new ArrayList<UserEvents>();

SoftwareDevelopmentEvent sde = new SoftwareDevelopmentEvent();
sde.setDescriptionDevelopment("Develop");
sde.setAdditionalInfoDevelopment("Development Test");
sde.setUserEventDate(new Date());
eventList.add(sde);

ComputerMaintenanceEvent cme = new ComputerMaintenanceEvent();
cme.setDescriptionMaintenance("Description maitnance");
cme.setAdditionalInfoMaintenance(" Maitnance Test");
cme.setUserEventDate(new Date());
eventList.add(cme);

TheUser user = new TheUser();
user.setUserFirstName("Vadim1");
user.setUserLastName("Dem1");
user.setUserGender("Male1");
user.setUserEmail("vad@mail.ru");
user.setPersonAddress("Adress");
user.setPersonPhoneNumber("1111111");

user.setUserEventsList(eventList);

dao.saveUser(user);

DAO方法很简单:

public void saveUser(TheUser user) {
    Session session = sessionFactory.getCurrentSession();
    session.save(user);
}
4

1 回答 1

0

请在此处查看我的帖子。

Java - Hibernate @ManyToMany 映射仅在一个方向上添加数据库中的记录

您需要设置关系的双方。

例如 cme.setUser(user);

艾伦

于 2013-10-31T13:08:45.163 回答