Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate swap out embeddable class for migrating existing data
    primarykey
    data
    text
    <p>I'm at a loss here, and perhaps it's something obvious because my Hibernate expertise is weaker than other areas.</p> <p>In legacy code there is a Hibernate <code>@Entity</code> class <code>Foo</code>. One of the its properties is this:</p> <pre><code>private OldBar bar = new OldBar(); </code></pre> <p><code>OldBar</code> is an <code>@Embeddable</code> class that uses a single column, <code>foobar</code>:</p> <pre><code>@Embeddable public class OldBar { private String fooBar; @Column(length = 10, nullable = false) private String getFooBar() { return fooBar; } @SuppressWarnings("unused") private void setFooBar(String fooBar) { this.fooBar = fooBar; } } </code></pre> <p>The original problem is that I needed to do something with <code>OldBar.fooBar</code>, but the original design had limitations and had this field private, preventing me from subclassing it, so I had to create a whole other class, <code>NewBar</code>, to replace the other one and get access to the private field. I thought that since <code>NewBar</code> is also <code>Embeddable</code> and has the same <code>@Column</code> designation, I could just swap out the field in the class <code>Foo</code>:</p> <pre><code>private NewBar bar = new NewBar(); </code></pre> <p>I wanted to do this because I have existing data in the <code>foobar</code> column, and I wanted to transparently use this data with <code>NewBar</code> instead of <code>OldBar</code>.</p> <p>Through trace logs I've seen that <code>Foo()</code> is created, with its default version of <code>NewBar()</code>, as one would expect, when the constructor is called. However, by the time code calls <code>Foo.getBar()</code>, for some reason <code>bar</code> is <code>null</code>! I'm supposing Hibernate is setting it to <code>null</code> for some reason---but why isn't Hibernate reading the data from the <code>foobar</code> column and creating an instance of <code>NewBar</code>? Why does it star working again when I put <code>OldBar</code> back in in place of <code>NewBar</code>? Surely there's nothing in the database itself that says which of the <code>@Embeddable</code> classes is mapped to the column, is there?</p> <p><em>Update:</em> This gets stranger and stranger. Sometimes I'll let the code overnight, and the next day it works! Or the next day it doesn't work! Just now it didn't work (that is, the <code>foobar</code> property was set to <code>null</code> instead of the value in the database), so I made class <code>ExactCopyOfOldBar</code> and put it in the place of <code>OldBar</code>. It worked fine! So I switch back to <code>NewBar</code>---merely undoing my temporary changes. It still worked, when it hadn't before! Is there some sort of cache where Hibernate serializes values and doesn't get them from the database? This is very odd.</p> <p><em>Update:</em> Now I can no longer get <code>NewBar</code> to work at all. I create <code>OtherBar</code>, which is basically identical to <code>NewBar</code> except that it has a different name, and I plug it in and it works, correctly reading the embedded string. I switch back out to <code>NewBar</code>, and I get <code>null</code> again. What is going on?</p> <p>Note that <code>Foo</code> is being loaded through <code>net.databinder.auth.hib.AuthDataApplication.getUser(String username)</code>, which is simple enough:</p> <pre><code>return (DataUser) Databinder.getHibernateSession().createCriteria(getUserClass()) .add(Restrictions.eq("username", username)).uniqueResult(); </code></pre> <p>I've verified over and over again that the <code>Foo</code> (user) table has a single row with the correct data, and most importantly that the <code>foobar</code> field has data. Why is Hibernate returning me a <code>Foo</code> with a <code>null</code> <code>foobar</code> field? Why does simply switching from <code>NewBar</code> to <code>OtherBar</code> make it start working again? Why does it work all day and then stop working after I've left it overnight?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload