-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate bug? to many queries
PostPosted: Thu Jan 19, 2006 10:26 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,
I came across, what I think is a bug but I want to verify this before moving it to JIRA.

Having a uni directional 1:n relation with a key where not-null=true, the following queries are generated, when I issue
Code:
Developer developer = new Developer();
Computer c = new Computer();
developer.getComputers().add(c);
session.save(developer);


This is one query to much and when performance is an issue, this is of course a problem. You can find the mapping and classes below. The problem is the same for set, bag, list and array.

Regards Sebastian

Code:
insert into tdeveloper (name, id) values (?, ?)
insert into tcomputer (name, developer_id, listindex, id) values (?, ?, ?, ?)

update tcomputer set developer_id=?, listindex=? where id=?


Developer:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.example1">
  <class name="Developer" table="tdeveloper">
    <id name="id">
      <generator class="sequence">
        <param name="sequence">tdeveloper_id_seq</param>
      </generator>
    </id>
    <property name="name" type="string"></property>
    <list name="computers" cascade="all" >
      <key column="developer_id" not-null="true"></key>
      <list-index column="listindex" ></list-index>
      <one-to-many class="Computer" />
    </list>
  </class>
</hibernate-mapping>

Developer class
Code:
package de.laliluna.example1;

public class Developer {
   private Integer id;
   private String name;
   private List computers = new ArrayList(); // mapped as list


   public List getComputers() {
      return computers;
   }

   public void setComputers(List computers) {
      this.computers = computers;
   }

   public Developer() {

   }

   public Integer getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

   public String toString() {
      return "Developer: " + getId() + " Name: " + getName();
   }



Computer:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.example1">
  <class name="Computer" table="tcomputer">
    <id name="id" >
      <generator class="sequence">
        <param name="sequence" >tcomputer_id_seq</param>
      </generator>
    </id>
    <property name="name" type="string"></property>
  </class>
</hibernate-mapping>

Class Computer
Code:
package de.laliluna.example1;

public class Computer {
   private Integer id;

   private String name;

   public Computer() {

   }

   public Integer getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

   public String toString() {
      return "Computer: " + getId() + " Name: " + getName();
   }
}

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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

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.