Results 1 to 5 of 5
  1. #1
    CChange is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Question @MappedSuperclass question (javax.persistence) - is it only allowed once?

    Hi!

    I have made an interesting discovery and was wondering whether I missed out something.

    I am using Hibernate for certain entities, and using @MappedSuperClass.

    ie. I have a base class (simplified):

    @MappedSuperClass
    public class BaseEntity
    {
    @Id
    @Column(name="record_id")
    String id;

    @Column(name="record_timestamp")
    Date timestamp;
    (.... getters, setters and so forth)
    }

    I then have derived another class
    @Entity
    @Table(name="report_company")
    public class Company extends BaseEntity
    {

    @Column(name="business_date")
    Date businessDate;
    ...getters/setters...
    }

    that works perfectly, the Company entity is retrieved nicely

    But if I want to add one more layer, and change the Company (so level 2) class to be an "abstract" one (in the perstence sense)

    @MappedSuperclass
    public class Company extends BaseEntity
    {
    @Column(name="business_date")
    Date businessDate;
    ...getters/setters...
    }


    and create another class (level 3)

    @Entity
    @Table(name="report_company")
    public class ReportCompany extends BaseEntity
    {
    (class is empty, because all I need here is the table name in the annotation)
    }

    It does NOT work, nothing happens in my code, dont even get an error, but if I query for ReportCompany entities I always get an empty list

    Where is the logic behind that, can @MappedSuperclass only be used once per hirarchy, and why does the reference not mention that??

    Thanks a lot in advance, once again.

    Cheers,
    CC

    ps.: Why I need this is simple, I have got a few fields common to every entity (id, status, ..), but then I also got three "Company" tables (actually functions) which look exactly the same, but should create three different objects - and I dont want to define them redundantly, it would seem really wrong)

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read about the default inheritance strategy applied if you don't state one and what the discriminator value used will be. This should make you see why the search for ReportCompany entities doesn't return anything.

  3. #3
    CChange is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Lightbulb bling

    >r035198x

    You clearly are the quickest tech-forum replier ever! Thanks mate!

    In this case it causes my embarrassment though, the issue waaaas *drumroll*, that I did not add the "level 3" class (ReportCompany) to my list of mapped classes (currently in a HibernateUtil class).

    It might make sense to still leave this post in here, if people search at least they know this MappedSuperclass is defo possible!


    Sorry, cheers though!

    CC

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You still need to verify your inheritance strategy though and I suggest stating it explicitly in your root entity class (where the table annotation is).

  5. #5
    CChange is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. problem with Persistence.createEntityManagerFactory
    By jordanthompson in forum Advanced Java
    Replies: 7
    Last Post: 12-24-2009, 12:38 PM
  2. Name persistence is not bound in this Context
    By tascoa in forum Java Servlet
    Replies: 1
    Last Post: 12-15-2009, 10:01 PM
  3. javax.microedition.io/ javax.bluetooth
    By ahtiven in forum New To Java
    Replies: 3
    Last Post: 01-13-2009, 02:54 PM
  4. JDO - defining the persistence using metadata
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 07:50 AM
  5. Container Managed Persistence
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 10:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •