-->
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: Trouble with freshly inserted row with many-to-one relation
PostPosted: Wed Nov 04, 2009 4:42 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
I'm reverse engineering a data table with composite natural keys, and a many-to-one relationship to a lookup table that associates a string with a field of the id.
Trouble is, the generated class does not fetch the associated strings.

Details:

hibernate.reveng.xml snippet:
Code:
<table name="HF_DI_ARTDSP">
  <primary-key>
   <key-column name="AOFIRM" />
   <key-column name="AOSTOR" />
   <key-column name="AOIDENTNR" />
  </primary-key>
  <foreign-key foreign-table="HF_DI_LI_FIRMA">
   <column-ref local-column="AOFIRM" foreign-column="FIFIRM" />
   <many-to-one update="true" insert="true" />
   <set/>
  </foreign-key>
</table>
<table name="HF_DI_LI_FIRMA">
  <primary-key>
   <key-column name="FIFIRM" />
  </primary-key>
</table>


HF_DI_LI_FIRMA also as an FITEXT field that contains the display string associated with a (numeric) FIRM id.

The generated class looks like this:

Code:
@Entity
@Table (name = "HF_DI_ARTDSP")
public class HfDiArtdsp implements java.io.Serializable {
  private HfDiArtdspId id;
  private HfDiLiStandort hfDiLiStandort;
  ...

  @ManyToOne (fetch = FetchType.LAZY)
  @JoinColumns ( {
      @JoinColumn (name = "AOFIRM", referencedColumnName = "STFIRM", nullable = false, insertable = false, updatable = false),
      @JoinColumn (name = "AOSTOR", referencedColumnName = "STSTOR", nullable = false, insertable = false, updatable = false)
  })
  public HfDiLiStandort getHfDiLiStandort () {
    return this.hfDiLiStandort;
  }
  public void setHfDiLiStandort (HfDiLiStandort hfDiLiStandort) {
    this.hfDiLiStandort = hfDiLiStandort;
  }
  ...


Now... how to I generate a new row for this?
I have this:

Code:
  row = new HfDiArtdsp ();
  row.setId (new HfDiArtdspId (5, 1, "123456")); -- sample values for primary key
  row.save ();
  row.refresh ();
  System.out.println (row.getHfDiLiStandort ()); -- prints "null"


No row with AOFIRM=5, AOSTOR=1, AOIDENTNR='123456' exists when this is run.
A row with FIFIRM=5 does exist in HF_DI_LI_FIRMA.

Running
Code:
  row = (HfDiArtdsp) session.load (HfDiArtdsp.class, new HfDiArtdspId (1, 1, "123456"));
  System.out.println (row.getHfDiLiStandort ()); -- prints a non-null object

does work if a row with AOFIRM=1, AOSTOR=1, AOIDENTNR='123456' does exist. So somehow the hfDiLiStandort field of row is not initialized with an on-demand-loading proxy during save(), it seems.

Maybe it's a problem just with fields from a composite id?
I haven't tried whether the effect exists for non-id fields.

I'm also utterly confused about the various attributes that you can set for <many-to-one> and <set> in the hibernate.reveng.xml. There's a bewildering array of options, and it's unclear which option does what. Googling gave me pages that simply listed the options, which didn't help.

Any ideas and pointers welcome.


Last edited by Joachim Durchholz on Thu Nov 05, 2009 12:23 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Trouble with freshly inserted row with many-to-one relation
PostPosted: Thu Nov 05, 2009 12:24 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
I have replaced the pseudocode with copies from the real code.
More noise, less errors. Hopefully this makes it easier to identify the problem.

[UPDATE]
I'm currently creating the row in the database, using createSQLQuery to run an INSERT statement, then retrieving the row via session.get.
It's a kludge, but it works, and as an added bonus, I don't have to initialize the fields that already have a default value configured in the database (of which there are two dozen or so). The code looks like this:

Code:
        session.createSQLQuery (
            "insert into hf_di_artdsp " +
            "(aofirm, aostor, aoidentnr, aobenp, aoladt, aostat) " +
            "values (:firm, :stor, :identnr, :benp, :ladt, :stat)")
            .setShort  ("firm", id.getAofirm ())
            .setShort  ("stor", id.getAostor ())
            .setString ("identnr", id.getAoidentnr ())
            .executeUpdate ();


Notice that all fields except the key fields are initialized by the database, something that is not possible when using the more normal row = new Artdsp (); row.setId (...); session.save (row) sequence.

[UPDATE 2]

The row.setId () call is required because the database uses natural, composite keys, so the usual key generator does not work.
This follows the patterns outlined in http://docs.jboss.org/hibernate/core/3. ... ce/en/html .

I'm not sure what struck you as strange in row.getHfDiLiFirma ().


Top
 Profile  
 
 Post subject: Re: Trouble with freshly inserted row with many-to-one relation
PostPosted: Mon Nov 09, 2009 2:28 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Oh. Right. Sorry for the mixup.

That should have been
Code:
  Session session = getSessionFactory ().getCurrentSession ();
  Transaction t = session.beginTransaction ();
  ...
  row = new HfDiArtdsp ();
  row.setId (new HfDiArtdspId (5, 1, "123456")); -- sample values for primary key
  session.save (row); -- persist the new object
  System.out.println (row.getHfDiLiStandort ()); -- prints "null"
  session.refresh (row);
  System.out.println (row.getHfDiLiStandort ()); -- still prints "null"
  ...
  t.commit ();

(omitting various try...catch constructs here for conciseness)


Top
 Profile  
 
 Post subject: Solved
PostPosted: Tue Nov 10, 2009 9:30 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
I forgot to say session.flush ().


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.