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

    Question "Dynamic" Java Annotations - possible?

    Hi everybody!!

    Not sure if that is even a question that makes sense, it definately does for me.

    I have a hibernate entity (so a pojo with annotations), which though only reads from the database and in this particular case does not read from a table, but from a function (postgresql).

    The Hibernate annotation goes like this:

    @Entity
    @Table(name="get_report_entity('T')")
    public class ReportEntity extends BaseEntity
    {...}

    As you can see the sql function (stored procedure) is "get_report_entity('T')". Now my problem - the 'T' as the parameter stands for "Test mode". And in production this should change to a 'P'.

    So the question is - is there a way for me to make that 'T' configurable, ie change that from the outside without having to recompile the whole code?? So ideally access an environment variable or a config file or something...

    Or do I have to switch to an xml mapping...p l e a s e n o t!

    Thanks very much!!

    Cheers,
    CC

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

    Default

    Quote Originally Posted by CChange View Post
    ...
    Or do I have to switch to an xml mapping...p l e a s e n o t!

    Thanks very much!!

    Cheers,
    CC
    Yes you have to. Create a dev-custommapping.xml and a prod-custommapping.xml to overrride the table names.

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

    Default

    Ok, thanks very much!

    Can I ask you (on your opinion on) one to two more question related to the above:

    (Sry, I know I can and will read this up, but as u are so quick) Is it possible to mix xml mapping and annotations, ie have the fieldmappings defined as annotations, and the table mappings in the xml?


    If not -

    What would you think of the solution of having a baseclass (class ReportEntity) without any hibernate table-annotation, and from it extend two empty child classes (class ReportEntityProd and class ReportEntityTest), and then use a factory to create the ReportEntity class which in turn uses a config file to choose whether to instantiate ReportEntityProd.class or ReportEntityTest.class. Does that make sense, or would that mess up hibernate internally??


    Very much appreciated!
    Cheers,
    CC

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

    Default

    XML can be mixed annotations. XML information overrides annotations when a conflict occurs.
    You should not have dev specific classes deployed together with your production code on your production environment. Those database settings are much like the information you provide in your datasource xml file in your server's deploy so that should go into an xml file as well.

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

    Default

    Thanks!

    Thats what I ll do then, keep the field definitions in the java file (for now), and the table mappings in the xml!

    Cheers!

    btw, its a Desktop Environment (ie a Swing/Jide based app), hibernate still works like a charm though!

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

    Thumbs up

    ps.: Thanks for the very quick replies

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

    Default

    You can still keep most of your table annotations in the code.
    You only need the DB specific stuff in the XML files to override the code stuff.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •