-->
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: Saving one record issues 4 additional queries
PostPosted: Tue Aug 11, 2009 6:35 am 
Newbie

Joined: Tue Aug 11, 2009 6:23 am
Posts: 3
Hello,

I have simple configuration. One "parent" table where I want to save record and several child tables related many-to-one to parent table.
I have created parent record in this way:
Code:
Jewel j = new Jewel(new Definition(-1), new DataModel(-1), new Model(-1), new StorageItem(-1), "test jewel",   0, "some title", "some description", new Long(-1), new Integer(0), new Integer(0));


so my child entities are Definition, DataModel, Model and StorageItem. In those tables there are dummy records with -1 primary keys.

The result is following:

Code:
Hibernate:
    select
        nextval ('JEWEL_SEQ')
Hibernate:
    /* get current state de.triplex.xgem.model.DataModel */ select
        datamodel_.data_model_id,
        datamodel_.length as length3_,
        datamodel_.reworking as reworking3_
    from
        data_model datamodel_
    where
        datamodel_.data_model_id=?
Hibernate:
    /* get current state de.triplex.xgem.model.Definition */ select
        definition_.definition_id,
        definition_.is_used as is2_7_,
        definition_.name as name7_
    from
        definition definition_
    where
        definition_.definition_id=?
Hibernate:
    /* get current state de.triplex.xgem.model.Model */ select
        model_.model_id,
        model_.xml as xml30_
    from
        model model_
    where
        model_.model_id=?
Hibernate:
    /* get current state de.triplex.xgem.model.StorageItem */ select
        storageite_.storage_item_id,
        storageite_.material_key as material2_38_,
        storageite_.name as name38_,
        storageite_.occasion_key as occasion4_38_,
        storageite_.shape_key as shape5_38_,
        storageite_.size_key as size6_38_,
        storageite_.style_key as style7_38_,
        storageite_.subtype_key as subtype8_38_,
        storageite_.surface_key as surface9_38_,
        storageite_.trend_key as trend10_38_,
        storageite_.type_key as type11_38_,
        storageite_.weight as weight38_
    from
        storage_item storageite_
    where
        storageite_.storage_item_id=?
Hibernate:
    /* insert de.triplex.xgem.model.Jewel
        */ insert
        into
            jewel
            (changeable, data_model_id, definition_id, description, is_active, is_created_by_admin, jewel_name, model_id, storage_item_id, title, user_id, jewel_id)
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


Is it possible to do just insert without selecting all other records in other 4 tables. How to forbid cascade actions I don't want to have ?

Here is mapping for Jewel and Model classes, for other three tables is the same:

Code:
package de.triplex.xgem.model;

// Generated 21.07.2009 15:05:48 by Hibernate Tools 3.2.2.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import de.triplex.xgem.model.JewelDetailedImage;
import de.triplex.xgem.model.JewelOverviewImage;

/**
* Jewel generated by hbm2java
*/
@Entity
@Table(name = "jewel")
public class Jewel implements java.io.Serializable {

   private Long jewelId;
   private Definition definition;
   private DataModel dataModel;
   private Model model;
   private StorageItem storageItem;
   private String jewelName;
   private int isActive;
   private String title;
   private String description;
   private Long userId;
   private Integer changeable;
   private Integer isCreatedByAdmin;
   private Set colors = new HashSet(0);
   private Set jewelCollections = new HashSet(0);
   private Set jewelWishLists = new HashSet(0);
   private Set jewelPoolHasGemElementGroups = new HashSet(0);
   private Set jewelPoolHasGemElements = new HashSet(0);
   private Set gemElementIsReplaceableBies = new HashSet(0);
   private Set<JewelOverviewImage> jewelOverviewImages = new HashSet<JewelOverviewImage>(0);
   private Set<JewelDetailedImage> jewelDetailedImages = new HashSet<JewelDetailedImage>(0);

   public Jewel() {
   }

   public Jewel(Long jewelId, int isActive) {
      this.jewelId = jewelId;
      this.isActive = isActive;
   }

   public Jewel(Long jewelId, Definition definition, DataModel dataModel,
         Model model, StorageItem storageItem, String jewelName,
         int isActive, String title, String description, Long userId,
         Integer changeable, Integer isCreatedByAdmin, Set colors,
         Set jewelCollections, Set jewelWishLists,
         Set jewelPoolHasGemElementGroups, Set jewelPoolHasGemElements,
         Set gemElementIsReplaceableBies) {
      this.jewelId = jewelId;
      this.definition = definition;
      this.dataModel = dataModel;
      this.model = model;
      this.storageItem = storageItem;
      this.jewelName = jewelName;
      this.isActive = isActive;
      this.title = title;
      this.description = description;
      this.userId = userId;
      this.changeable = changeable;
      this.isCreatedByAdmin = isCreatedByAdmin;
      this.colors = colors;
      this.jewelCollections = jewelCollections;
      this.jewelWishLists = jewelWishLists;
      this.jewelPoolHasGemElementGroups = jewelPoolHasGemElementGroups;
      this.jewelPoolHasGemElements = jewelPoolHasGemElements;
      this.gemElementIsReplaceableBies = gemElementIsReplaceableBies;
   }
   
   public Jewel(Definition definition, DataModel dataModel,
         Model model, StorageItem storageItem, String jewelName,
         int isActive, String title, String description, Long userId,
         Integer changeable, Integer isCreatedByAdmin) {
      this.definition = definition;
      this.dataModel = dataModel;
      this.model = model;
      this.storageItem = storageItem;
      this.jewelName = jewelName;
      this.isActive = isActive;
      this.title = title;
      this.description = description;
      this.userId = userId;
      this.changeable = changeable;
      this.isCreatedByAdmin = isCreatedByAdmin;
   }   

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="jewel_sequence")
   @SequenceGenerator(name="jewel_sequence",sequenceName="JEWEL_SEQ", allocationSize=1)      
   @Column(name = "jewel_id", unique = true, nullable = false)
   public Long getJewelId() {
      return this.jewelId;
   }

   public void setJewelId(Long jewelId) {
      this.jewelId = jewelId;
   }

   @ManyToOne(fetch = FetchType.LAZY, cascade = {})
   @JoinColumn(name = "definition_id")
   public Definition getDefinition() {
      return this.definition;
   }

   public void setDefinition(Definition definition) {
      this.definition = definition;
   }

   @ManyToOne(fetch = FetchType.LAZY, cascade = {})
   @JoinColumn(name = "data_model_id")
   public DataModel getDataModel() {
      return this.dataModel;
   }

   public void setDataModel(DataModel dataModel) {
      this.dataModel = dataModel;
   }

   @ManyToOne(fetch = FetchType.LAZY, cascade = {})
   @JoinColumn(name = "model_id")
   public Model getModel() {
      return this.model;
   }

   public void setModel(Model model) {
      this.model = model;
   }

   @ManyToOne(fetch = FetchType.LAZY, cascade = {})
   @JoinColumn(name = "storage_item_id")
   public StorageItem getStorageItem() {
      return this.storageItem;
   }

   public void setStorageItem(StorageItem storageItem) {
      this.storageItem = storageItem;
   }

   @Column(name = "jewel_name", length = 100)
   public String getJewelName() {
      return this.jewelName;
   }

   public void setJewelName(String jewelName) {
      this.jewelName = jewelName;
   }

   @Column(name = "is_active", nullable = false)
   public int getIsActive() {
      return this.isActive;
   }

   public void setIsActive(int isActive) {
      this.isActive = isActive;
   }

   @Column(name = "title", length = 100)
   public String getTitle() {
      return this.title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   @Column(name = "description", length = 500)
   public String getDescription() {
      return this.description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   @Column(name = "user_id")
   public Long getUserId() {
      return this.userId;
   }

   public void setUserId(Long userId) {
      this.userId = userId;
   }

   @Column(name = "changeable")
   public Integer getChangeable() {
      return this.changeable;
   }

   public void setChangeable(Integer changeable) {
      this.changeable = changeable;
   }

   @Column(name = "is_created_by_admin")
   public Integer getIsCreatedByAdmin() {
      return this.isCreatedByAdmin;
   }

   public void setIsCreatedByAdmin(Integer isCreatedByAdmin) {
      this.isCreatedByAdmin = isCreatedByAdmin;
   }

   @ManyToMany(cascade = {}, fetch = FetchType.LAZY, targetEntity=Color.class)
   @JoinTable(name = "jewel_has_hair_color", joinColumns = { @JoinColumn(name = "jewel_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "color_id", nullable = false, updatable = false) })
   public Set getColors() {
      return this.colors;
   }

   public void setColors(Set colors) {
      this.colors = colors;
   }

   @ManyToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewels", targetEntity=JewelCollection.class)
   public Set getJewelCollections() {
      return this.jewelCollections;
   }

   public void setJewelCollections(Set jewelCollections) {
      this.jewelCollections = jewelCollections;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel", targetEntity=JewelWishList.class)
   public Set getJewelWishLists() {
      return this.jewelWishLists;
   }

   public void setJewelWishLists(Set jewelWishLists) {
      this.jewelWishLists = jewelWishLists;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel", targetEntity=JewelPoolHasGemElementGroup.class)
   public Set getJewelPoolHasGemElementGroups() {
      return this.jewelPoolHasGemElementGroups;
   }

   public void setJewelPoolHasGemElementGroups(Set jewelPoolHasGemElementGroups) {
      this.jewelPoolHasGemElementGroups = jewelPoolHasGemElementGroups;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel", targetEntity=JewelPoolHasGemElement.class)
   public Set getJewelPoolHasGemElements() {
      return this.jewelPoolHasGemElements;
   }

   public void setJewelPoolHasGemElements(Set jewelPoolHasGemElements) {
      this.jewelPoolHasGemElements = jewelPoolHasGemElements;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel", targetEntity=GemElementIsReplaceableBy.class)
   public Set getGemElementIsReplaceableBies() {
      return this.gemElementIsReplaceableBies;
   }

   public void setGemElementIsReplaceableBies(Set gemElementIsReplaceableBies) {
      this.gemElementIsReplaceableBies = gemElementIsReplaceableBies;
   }
   
   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel")
   public Set<JewelOverviewImage> getJewelOverviewImages() {
      return this.jewelOverviewImages;
   }

   public void setJewelOverviewImages(Set<JewelOverviewImage> jewelOverviewImages) {
      this.jewelOverviewImages = jewelOverviewImages;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "jewel")
   public Set<JewelDetailedImage> getJewelDetailedImages() {
      return this.jewelDetailedImages;
   }

   public void setJewelDetailedImages(Set<JewelDetailedImage> jewelDetailedImages) {
      this.jewelDetailedImages = jewelDetailedImages;
   }
   

}


Model:

Code:
package de.triplex.xgem.model;

// Generated 21.07.2009 15:05:48 by Hibernate Tools 3.2.2.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* Model generated by hbm2java
*/
@Entity
@Table(name = "model")
public class Model implements java.io.Serializable {

   private long modelId;
   private byte[] xml;
   private Set jewels = new HashSet(0);

   public Model() {
   }

   public Model(long modelId) {
      this.modelId = modelId;
   }

   public Model(long modelId, byte[] xml, Set jewels) {
      this.modelId = modelId;
      this.xml = xml;
      this.jewels = jewels;
   }

   @Id
   @Column(name = "model_id", unique = true, nullable = false)
   public long getModelId() {
      return this.modelId;
   }

   public void setModelId(long modelId) {
      this.modelId = modelId;
   }

   @Column(name = "xml")
   public byte[] getXml() {
      return this.xml;
   }

   public void setXml(byte[] xml) {
      this.xml = xml;
   }

   @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "model", targetEntity=Jewel.class)
   public Set getJewels() {
      return this.jewels;
   }

   public void setJewels(Set jewels) {
      this.jewels = jewels;
   }

}


I've really have no idea how to stop this. One way I know would be to break those relations, but why to use Hibernate then in the first place. I need better solution.

Thanks in advance.

Ljubisa


Top
 Profile  
 
 Post subject: Re: Saving one record issues 4 additional queries
PostPosted: Thu Aug 13, 2009 4:56 am 
Newbie

Joined: Tue Aug 11, 2009 6:23 am
Posts: 3
I still have this problem, I've tryied a number of different configurations and always at the end I get the same result. Is there any way to prevent those additional selects? Why Hibernate does this by default?

Regards,
Ljubisa


Top
 Profile  
 
 Post subject: Re: Saving one record issues 4 additional queries
PostPosted: Thu Aug 13, 2009 6:46 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
It is no problem, it's as designed.
You tell it to create a reference to specific rows in the child tables, so those are fetched and loaded into the entity in memory (and you'd get an error if they weren't there and couldn't be created).

If you don't want that, you will have to omit the colums from the insert. You may modify the table with an insert trigger to add the references at database level, which would of course leave your entity in an invalid state unless you refresh it (which would load the dependencies, unless you set them to lazy loading).

The lesson here is to not do this. If something doesn't exist, leave it to NULL in the database and set the field to "default NULL".


Top
 Profile  
 
 Post subject: Re: Saving one record issues 4 additional queries
PostPosted: Fri Aug 14, 2009 7:39 am 
Newbie

Joined: Tue Aug 11, 2009 6:23 am
Posts: 3
Hello,

Thank you jwenting very much for the clarification.

I will need to think more about this. Thing is that I have indexes over those columns and because of that I'm using -1 values. They are replacements for null values. With such technique all records are indexed and I don't have a problem with the performances latter, this is technique I'm using for a years. Thank you.

Regards,
Ljubisa


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.