问题描述:

I have a hibernate-mapped data structures and there is an error when I try to delete it.

@Entity

@Table(name = "TYPE")

public class Type {

@Id

private int id;

private String name;

}

@Entity

@Table(name = "BOOKS")

public class Book {

@Id

private int id;

@ManyToOne(fetch = FetchType.EAGER, optional = false)

private Type type;

}

When I try to remove Book object I got

Caused by: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com....Book.type; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com...Book.type

at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:645)

at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)

at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)

at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)

at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:846)

at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:842)

When I remove optional = false parameter I see that Hibernate deletes objects from TYPES tables. Why it tries to remove type entities, if there is ManyToOne link from oposite side?

Thanks.

upd:

Collections of books is available in parent object. To remove it I use this code:

Information info = getInformationById(id);

if (info != null) {

getHibernateTemplate().delete(info);

}

upd2 (full hierarchy):

@Entity

@Table(name = "INFO")

public class Information {

@Id

@Column(name = "ID")

private int id;

@Version

private int version;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

@JoinColumn(name = "INFO_ID")

private Set<Details> details;

...

}

@Entity

@Table(name = "DETAILS")

public class Details {

@Id

private int id;

@Version

private int version;

@Column(name = "INFO_ID")

private int infoId;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

@JoinColumn(name = "DETAILS_ID", nullable = false)

private Set<Books> books = new HashSet<Book>();

...

}

Chain is: Info->Details->Books. Each book has a type, it is possible, that many books have the same type. I don't want to delete type entities, when I delete info objects.

相关阅读:
Top