Добрый день. Имею следующую проблему:
org.hibernate.id.IdentifierGenerationException: null id generated for:class Notebook.Enities.Telephone
Суть понятна, дебаг подтверждает: в id у нас null у telephone.
GitHub:
NotebookСУБД MySQL.
Поле Id: Id int(11) not null;
Класс Note.java
@Component
public class Note {
private int id;
private Description descriptionId;
private Human humanId;
private Telephone telephoneId;
private Group group;
public Note (){
}
public Note(int id, Description descriptionId, Human humanId, Telephone telephoneId, Group group) {
this.id = id;
this.descriptionId = descriptionId;
this.humanId = humanId;
this.telephoneId = telephoneId;
this.group = group;
}
/************сеттеры и геттеры*****************/
Класс Telephone.java
@Component
public class Telephone {
private Integer id;
private String telephone;
private String descripton;
private Note note;
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
}
public Telephone(int id, String telephone, String descripton) {
this.id = id;
this.telephone = telephone;
this.descripton = descripton;
}
public Telephone() {
}
public Telephone(String telephone) {
this.telephone = telephone;
}
/*******************геттеры и сэттеры*************/
Action-исполнитель: NotebookFacade.javapackage Notebook.Objects;
import Notebook.DAO.interfaces.getNoteDAO;
import Notebook.Enities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Scope("singleton")
public class NotebookFacade {
@Autowired
private getNoteDAO getNoteDAO;
@Autowired
private SearchCriteria searchCriteria;
private List<Note> notes;
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!");
}
}
И сам исполнитель, который и ругается.package Notebook.DAO.impl;
import Notebook.DAO.interfaces.getNoteDAO;
import Notebook.Enities.*;
import Notebook.Utils.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.*;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by Ковальчук on 30.03.2016.
*/
@Component
public class GetNoteDAOImpl implements getNoteDAO {
@Autowired
private SessionFactory sessionFactory;
private ProjectionList noteProjectionList;
/****************************** Методы получения *********************************/
/****************************** Методы удаления, изменения записей *********************************/
@Transactional
@Override
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();
}
@Transactional
@Override
public void deleteNotes(Note note) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.delete(note.getId());
session.flush();
session.getTransaction().commit();
}
}
Telephone.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.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>
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 class="Notebook.Enities.Telephone" name="telephoneId" 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>
Дампы: Телефон
DROP TABLE IF EXISTS `telephone`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `telephone` (
`Id` bigint(20) NOT NULL AUTO_INCREMENT,
`Telephone` varchar(15) NOT NULL,
`Descripton` varchar(45) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Human telephone';
Note
CREATE TABLE `note` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Description_id` int(11) NOT NULL,
`Human_id` int(20) DEFAULT NULL,
`Telephone_id` bigint(20) NOT NULL,
`Group` int(20) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `fk_human_idx` (`Human_id`),
KEY `fk_description_idx` (`Description_id`),
KEY `fk_group_idx` (`Group`),
KEY `fk_telephone_idx` (`Telephone_id`),
CONSTRAINT `fk_description` FOREIGN KEY (`Description_id`) REFERENCES `description` (`Id`) ON UPDATE CASCADE,
CONSTRAINT `fk_group` FOREIGN KEY (`Group`) REFERENCES `group` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_human` FOREIGN KEY (`Human_id`) REFERENCES `human` (`id`) ON UPDATE CASCADE,
CONSTRAINT `fk_telephone` FOREIGN KEY (`Telephone_id`) REFERENCES `telephone` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
Файлы немного подрезаны (убран остальной функционал, непричастный).
Пожалуйста, помогите исправить ошибку.