-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate loading dependencies
PostPosted: Thu Sep 08, 2011 11:24 am 
Newbie

Joined: Thu Sep 08, 2011 11:12 am
Posts: 2
Hi there,

i am fairly new to hibernate and have to work into an spring/hibernate application structure which should be used in future in addition to an existing php application.

Now, the problem is, we have tables which have a primary key called 'autoid' and a "real" primary key which is a combination of two columns 'id' and 'endstempel_id'.

I have a Unternehmen Entity:
Code:
@Entity
@Table(name="unternehmen")
@ZmuekTable(isHistory=false)
@SuppressWarnings("serial")
@Proxy(lazy=true)
public class Unternehmen implements Serializable{
    @Column(name="unternehmen_autoid")
    @Id
    @GeneratedValue
    private Integer id;
.....
    @OneToOne(targetEntity=net.zmuek.backend.domainmodel.adresse.Adresse.class, fetch=FetchType.LAZY, optional=false)
   @JoinColumn(name="unternehmen_hauptadresse_id", referencedColumnName="adresse_id")
    private Adresse hauptAdresse;
}


and the adresse table:
Code:
@Entity
@Table(name="adresse")
@ZmuekTable(isHistory=true)
@SuppressWarnings("serial")
@Where(clause="adresse_endstempel_id = 3999999999")
public class Adresse implements Serializable{
    @Column(name="adresse_autoid")
    @Id
    @GeneratedValue
    private Integer rawId;

    @Column(name="adresse_id")
    private Integer id;

    @Column(name="adresse_endstempel_id")
    private String endstempelId;
...
}


Now, when i load an Unternehmen entity hibernate also loads the adresse entity, despite the fact i told it to lazy load.
I found out that our primary combination is the problem here.
If i use:
Code:
    @OneToOne(targetEntity=net.zmuek.backend.domainmodel.adresse.Adresse.class, fetch=FetchType.LAZY, optional=false)
    @JoinColumn(name="unternehmen_hauptadresse_id", referencedColumnName="adresse_autoid")
    private Adresse hauptAdresse;

...

@Entity
@Table(name="adresse")
@ZmuekTable(isHistory=true)
@SuppressWarnings("serial")
public class Adresse implements Serializable{
}


then everything works as expected.

How can i get around this?

When i load all Unternehmen from the database it loads every related entity and fires one single sql for every entity, which puts a heavy load onto the server.

Thanks in Advance
Sven


Top
 Profile  
 
 Post subject: Re: Hibernate loading dependencies
PostPosted: Fri Sep 09, 2011 8:34 am 
Newbie

Joined: Thu Sep 08, 2011 11:12 am
Posts: 2
Inbetween i found out that i can use multiple @Ids in one Entity and reference them.
This way lazy loading does work, but it offers us another problem.

In our tables we have the composition of two fields: 'id' and 'endstempel_id'. They are our primary key now.
We use these columns for history saving, and the latest entry is always the one with id:x and endstempel_id: 3999999999.

What i have to do, to make lazy loading work, is to add a foreign key column for every other table i reference too.

For my example this means i have to add a adresse_endstempel_id column in the unternehmen table, which defaults to (and always is): 3999999999.
This seems stupid to me, but this is the only thing i found so far.

And what seems worse is that, for every relation i have in my table (another adress, user, etc.) i have to add another foreign key column, which always has the entry 3999999999, which just causes my DB to get filled up with useless data.

This is the code i have now:
Code:
    @OneToOne(targetEntity=net.zmuek.backend.domainmodel.adresse.Adresse.class, fetch=FetchType.LAZY)
    @JoinColumns ({
      @JoinColumn(name="unternehmen_hauptadresse_id", referencedColumnName="adresse_id"),
      @JoinColumn(name="unternehmen_hauptadresse_endstempel_id", referencedColumnName="adresse_endstempel_id")
   })


and
Code:
    @Id
    @Column(name="adresse_endstempel_id")
    private Long adresse_endstempel_id;

    @Id
    @Column(name="adresse_id")
    private Long adresse_id;


Is there a way to work around this? What i imagine is to tell hibernate that the
Code:
@JoinColumn(name="unternehmen_hauptadresse_endstempel_id", referencedColumnName="adresse_endstempel_id")

is our fixed 3999999999 value.

Greetings
Sven


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.