2016-04-04 11 views
0

hepsi. Yani.için null id oluşturuldu: sınıf hibernate

boş kimliği için oluşturulmuş: Bir sorunum var

Evet, bu sorun StackOverflow olmuştur Notebook.Enities.Telephone sınıfı, ama buradayım. Niye ya? Xml eşlemesiyle düzenledim.

Benim Note.hbm.xml

<?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> 

İleri, benim Telephone.hbn.xml (Telefon - ru-lang için telefon.)

<?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> 

MTU-sayfa Text.xhtml (diğer nesneler tüm benzetimler Telefon)

<?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> 

Ve Yöntem createNote();

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!"); 
} 

ve addNotes();

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(); 
} 

Benim şeması: enter image description here

Başka bir şey gerekiyorsa, lütfen. Ve lütfen yardım edin.

cevap

0

Aşağıdaki bağlantı, hibernate ilişkisinin xml yapılandırmasıyla nasıl bütünleştirileceğini açıklar, bu yüzden lütfen dikkat edin.

Hibernate XML Mapping Example

Ayrıca

@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(); 
} 

sorununuzu çözecektir Umut @Transactional hizmet sınıfı yöntemi addNotes ile eklemeniz gerekir!

İlgili konular