-->
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: Migration To 5.2.4: Problem with inheritance
PostPosted: Fri Nov 11, 2016 6:31 am 
Newbie

Joined: Fri Nov 11, 2016 5:31 am
Posts: 4
Hi,

I try to migrate from Hibernate 5.0.5 to 5.2.4.

I have a problem with inheritance. It seems to me that Hibernate 5.2.4 does not recognize a column in a subclass and I don't know why.

My class implementation (not changed!):
Code:
@Entity(name = "A_Object")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DIS", discriminatorType = DiscriminatorType.STRING, length = 3)
@DiscriminatorValue("AD")
@Table(name = "A")
public class A {
...
private Integer nr = null;

  @Column(name = "A_NR")
  public Integer getNr() {
    return nr;
  }
  public void setNr(Integer val) {
    this.nr = val;
  }

...
}

@Entity(name = "B_Object")
@DiscriminatorValue("BD")
@SecondaryTable(name = "B", pkJoinColumns = @PrimaryKeyJoinColumn(name = "B_A_ID"))
public class B extends A {
...
  private Integer nr;

  @Override
  @Column(table="B", name = "B_NR")
  public Integer getNr() {
    return nr;
  }
  public void setNr(Integer val) {
    this.nr = val;
  }
...
}


When I try to save an instance of B, although B.nr is set (but not a.nr), in the trace the column "B_NR" is not included:

Code:
insert
    into
        TEST.A (A_COL1, A_COL_2, A_NR) // A_NR included! And set to "1", although a.nr is null and b.nr = "1"?!
     values (?, ?,...)
insert
    into
        TEST.B (B_COL1, B_COL_2, ...) // B_NR not included!
     values (?, ?,...)


The result is a ConstraintViolationException, because B_NR has a not-null constraint. (There is no constraint on A_NR)

When I rename B.getNr() to B.getNr2() so that it does not override A.getNr() any longer, everything works fine. A_NR is set to null, B_NR is set to "1". Hibernate recognizes the column B_NR and B_NR is included in the insert:

Code:
insert
    into
        TEST.A (A_COL1, A_COL_2, ..., A_NR) // A_NR included!
     values (?, ?,...)
insert
    into
        TEST.B (B_COL1, B_COL_2, ..., B_NR) // B_NR included!
     values (?, ?,...)

With Hibernate 5.0.5, this problem did not exist.

Can you help me please how to fix the problem?

Thank you in advance!

Btw:
Changing the implementation from B.setNr() to
Code:
  public void setNr(Integer val) {
    this.nr = val;
    super.setNr(val);
  }

does not solve the problem. Although a.nr and b.nr are set, the column B_NR is not detected/filled.


Top
 Profile  
 
 Post subject: Re: Migration To 5.2.4: Problem with inheritance
PostPosted: Fri Nov 11, 2016 6:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Hibernate properties are not meant to be extended the way you do.

So, the subclass B cannot change the class A property to map it to a different column.

Try with `@AttributeOverride` annotation:

Code:
Entity(name = "B_Object")
@DiscriminatorValue("BD")
@AttributeOverride(name="nr", column=@Column(table="B", name = "B_NR"))
@SecondaryTable(name = "B", pkJoinColumns = @PrimaryKeyJoinColumn(name = "B_A_ID"))
public class B extends A {

....

}

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Migration To 5.2.4: Problem with inheritance
PostPosted: Fri Nov 11, 2016 8:30 am 
Newbie

Joined: Fri Nov 11, 2016 5:31 am
Posts: 4
Hi Vlad,

thank you very much for your reply!

With Hibernate 5.0.5 (and previous) the result was, that columns A.A_NR and B.B_NR were filled.
With Hibernate 5.2.4, B.B_NR is ignored.

My first attempts with @AttributeOverride had no effect, Hibernate does not try to fill B.B_NR. Perhaps my problem is, that I annotate methods, not fields. Still thinking about it.

(My aim wasn't to change class A property to map it to a different column. My aim was that class A property is mapped to table A column a.a_nr, and class B property is mapped to table B column b.b_nr. But perhaps the problem is that I annotate methods - not fields - and b.getNr() overrides a.getNr()).


Top
 Profile  
 
 Post subject: Re: Migration To 5.2.4: Problem with inheritance
PostPosted: Fri Nov 11, 2016 8:53 am 
Newbie

Joined: Fri Nov 11, 2016 5:31 am
Posts: 4
Ok, the solution is to drop and forget B.B_NR and to migrate old database entries ;-)


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.