首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为:类hibernate生成的空id

为:类hibernate生成的空id
EN

Stack Overflow用户
提问于 2016-04-04 04:23:28
回答 1查看 455关注 0票数 0

全。所以。我有个问题:

为:类Notebook.Enities.Telephone生成的null id

是的,这个问题一直是堆积如山,但我在这里。为什么?我通过xml映射进行了安排。

My Note.hbm.xml

代码语言:javascript
复制
    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="Notebook.Enities.Note" table="note" schema="notebase">
        <id name="id" type="java.lang.Integer">
            <column name="Id" not-null="true"/>
            <generator class="identity"/>
        </id>
        <one-to-one name="telephoneId" class="Notebook.Enities.Telephone" cascade="save-update"/>
        <one-to-one class="Notebook.Enities.Description" name="descriptionId" cascade="save-update"/>
        <one-to-one class="Notebook.Enities.Human" name="humanId" cascade="save-update"/>
        <many-to-one class="Notebook.Enities.Group" name="group" fetch="join" lazy="false">
            <column name="Group" not-null="true"/>
        </many-to-one>
    </class>
</hibernate-mapping>

Next,my Telephone.hbn.xml (ru-lang的电话-电话)

代码语言:javascript
复制
    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="Notebook.Enities.Telephone" table="telephone" schema="notebase">
        <id name="id" type="java.lang.Integer">
            <column name="Id"/>
            <generator class="foreign">
                <param name="property">note</param>
            </generator>
        </id>
        <one-to-one name="note" class="Notebook.Enities.Note" constrained="true"/>
        <property name="telephone" column="Telephone"/>
        <property name="descripton" column="Descripton"/>
    </class>
</hibernate-mapping>

JSF-页面Text.xhtml (其他对象均为模拟电话)

代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui"
>

<body>
<f:view>
    <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
      <h:form>
        <h:panelGrid columns="2">

            <h:outputText value="Фамилия:"/>
            <h:inputText value="#{human.family}"/>
            <h:outputText value="Имя:"/>
            <h:inputText value="#{human.name}"/>
            <h:outputText value="Отчество:"/>
            <h:inputText value="#{human.secondName}"/>
            <h:outputText value="Дата:"/>
            <h:inputText value="#{human.birthday}">
                <f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
            </h:inputText>
            <h:outputText value="Телефон:"/>
            <h:inputText value="#{telephone.telephone}"/>
            <h:outputText value="Группа:"/>
            <h:inputText value="#{group.group}"/>
            <h:outputText value="Описание:"/>
            <h:inputText value="#{description.text}"/>

        </h:panelGrid>

        <h:commandButton action="#{notebookFacade.createNote(human,telephone,group,description)}" value="Save" />
        <h:commandButton action="descriptionList" value="Cancel"/>
        <br/>
    </h:form>
</f:view>
</body>
</html>

和方法createNote();

代码语言:javascript
复制
 public void createNote(Human human, Telephone telephone, Group group, Description description){
    Note note = new Note();
    note.setTelephoneId(telephone);
        telephone.setNote(note);
    note.setHumanId(human);
        human.setNote(note);
    note.setGroup(group);
    note.setDescriptionId(description);
        description.setNote(note);
    getNoteDAO.addNotes(note);
    System.out.println("Add work!");
}

和addNotes();

代码语言:javascript
复制
public void addNotes(Note note) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(note);
    session.getTransaction().commit();
    System.out.println("This function is worked");
    session.close();
}

我的计划:在这里输入图像描述

,如果您需要其他的东西,请。请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2016-04-04 06:31:00

下面的链接将解释如何将hibernate关系与xml配置集成起来,因此请注意。

Hibernate XML映射示例

此外,您还需要使用addNotes服务类方法添加@Transactional

代码语言:javascript
复制
@Transactional
public void addNotes(Note note) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(note);
    session.getTransaction().commit();
    System.out.println("This function is worked");
    session.close();
}

希望你能解决你的问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36394541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档