-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: One-to-many unidirectional problem
PostPosted: Wed Feb 02, 2011 4:28 pm 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Hi,

I have a one-to-many unidirectional relationship between two entities.

These are the classes:

Code:
@Entity
@Table(name = "test_document_types")
public class DocumentType {

   private Integer id;
   private String name;
   private Set<PropertyDefinition> propertyDefinitions = Sets.newHashSet();

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id")
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   @Column(name = "name")
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
   @OneToMany(
      cascade = CascadeType.ALL,
      orphanRemoval = true,
      fetch = FetchType.EAGER
   )
   @JoinColumn(name = "document_type_id")
   public Set<PropertyDefinition> getPropertyDefinitions() {
      return propertyDefinitions;
   }
   
   public void setPropertyDefinitions(Set<PropertyDefinition> propertyDefinitions) {
      this.propertyDefinitions = propertyDefinitions;
   }
}


Code:
@Entity
@Table(name = "test_property_definitions")
public class PropertyDefinition {

   private Integer id;
   private String name;
   
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id")
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   @Column(name = "name")
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}


This is the DDL:

Code:
CREATE TABLE test_document_types (

   id INT NOT NULL AUTO_INCREMENT,
   name VARCHAR(32) NOT NULL,
   
   PRIMARY KEY (id)
   
) ENGINE = InnoDB;

CREATE TABLE test_property_definitions (

   id INT NOT NULL AUTO_INCREMENT,
   document_type_id INT NOT NULL,
   name VARCHAR(32) NOT NULL,
   
   PRIMARY KEY (id),
   FOREIGN KEY (document_type_id) REFERENCES test_document_types (id)
   
) ENGINE = InnoDB;


As you can see, there is nothing fancy.
The problem is that an exception is thrown when I save a new DocumentType, because of the cascading operation.

This is an example of saving:

Code:
DocumentType documentType = new DocumentType();
documentType.setName("Document type 1");

PropertyDefinition propertyDefinition1 = new PropertyDefinition();
propertyDefinition1.setName("Property 1");
documentType.getPropertyDefinitions().add(propertyDefinition1);

PropertyDefinition propertyDefinition2 = new PropertyDefinition();
propertyDefinition2.setName("Property 2");
documentType.getPropertyDefinitions().add(propertyDefinition2);

getHibernateTemplate().saveOrUpdate(documentType);


... and produces the following output:

Hibernate: insert into test_document_types (name) values (?)
Hibernate: insert into test_property_definitions (name) values (?)
2011-02-02 21:54:20,937 [WARN] org.hibernate.util.JDBCExceptionReporter: SQL Error: 1364, SQLState: HY000
2011-02-02 21:54:20,937 [WARN] org.hibernate.util.JDBCExceptionReporter: SQL Error: 1364, SQLState: HY000
2011-02-02 21:54:20,937 [ERROR] org.hibernate.util.JDBCExceptionReporter: Field 'document_type_id' doesn't have a default value
2011-02-02 21:54:20,937 [ERROR] org.hibernate.util.JDBCExceptionReporter: Field 'document_type_id' doesn't have a default value


Looks like the value for the foreign key of the child is not set before saving.

Hibernate version: 3.5.6
DBMS: MySQL 5.1.41.0


Top
 Profile  
 
 Post subject: Re: One-to-many unidirectional problem
PostPosted: Wed Feb 02, 2011 4:57 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is covered in the Parent/Child example in the documentation (http://docs.jboss.org/hibernate/core/3. ... hild-bidir)

Basically you have two options. Either make the document_type_id column nullable or make a bi-directional relationship that is managed by the PropertyDefinition side. The example is with XML mapping files, and I am not very familiar with the annotations. Hopefully you can find out what the corresponding annotations are yourself.


Top
 Profile  
 
 Post subject: Re: One-to-many unidirectional problem
PostPosted: Sat Feb 05, 2011 5:05 am 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Thank you.

I solved the problem using a bi-directional relationship.

Code:
@Entity
@Table(name = "test_document_types")
public class DocumentType {

   ...
   private Set<PropertyDefinition> propertyDefinitions = Sets.newHashSet();
   
   ...
   
   @OneToMany(
      cascade = CascadeType.ALL, orphanRemoval = true,
      fetch = FetchType.EAGER, mappedBy = "documentType"
   )
   public Set<PropertyDefinition> getPropertyDefinitions() {
      return propertyDefinitions;
   }
   
   public void setPropertyDefinitions(Set<PropertyDefinition> propertyDefinitions) {
      this.propertyDefinitions = propertyDefinitions;
   }
   
   public void addPropertyDefinition(PropertyDefinition propertyDefinition) {
      propertyDefinition.setDocumentType(this);
      propertyDefinitions.add(propertyDefinition);
   }
}


Code:
@Entity
@Table(name = "test_property_definitions")
public class PropertyDefinition {

   ...
   private DocumentType documentType;
   
   ...
   
   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "document_type_id")
   public DocumentType getDocumentType() {
      return documentType;
   }
   
   public void setDocumentType(DocumentType documentType) {
      this.documentType = documentType;
   }
}


Top
 Profile  
 
 Post subject: Re: One-to-many unidirectional problem
PostPosted: Sun Feb 06, 2011 3:22 pm 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Unfortunately, I ran into the same problem when deleting a parent entity (and relying on the cascade).

When deleting (delete(documentType)), the following SQL gets executed first:
update property_definitions set document_type_id=null where document_type_id=?

And the same error message as before (and expected considering the SQL executed by Hibernate):
Column 'document_type_id' cannot be null

The mappings are in my previous post (where I said problem solved).
What am I doing wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.