1 2 Previous Next 23 Replies Latest reply: Feb 10, 2009 2:51 AM by Amin Mohammed-Coleman RSS

Inheritence

Amin Mohammed-Coleman Newbie

Hi

I'm not sure if this is a problem and it may be that I haven't implemented it properly. I have an super class Contact which has a set of address objects, telephone objects and notes objects. There are 2 subclasses PersonalContact and BusinessContact.

I have a service method that returns a list of revision entities for either personal contact or business contact. I wrote a test where I created a personalcontact and changed the name of the contact. the service method returns the correct history(showing the changes). I then added an address to the personalcontact object and then added another one but the history list doesn;t contain the address.

Here is the code to get the history



public List<? extends Contact> showContactHistory(long contactId, boolean asc) {
 Contact contact = contactDao.findById(contactId);
 @SuppressWarnings("unchecked")List<? extends Contact> history = (List<? extends Contact>) showEntityHistory(contact.getClass(), contact.getId(), asc);
 return history;
 }


 public List<?> showEntityHistory(Class<?> entityClass, Long entityId, boolean dateAsc) {
 VersionsReader reader = VersionsReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
 VersionsOrder versionOrder = dateAsc ? RevisionProperty.asc(): RevisionProperty.desc();
 List<?> result = reader.createQuery().forRevisionsOfEntity(entityClass,true, true).add(VersionsRestrictions.idEq(entityId)).addOrder(versionOrder).getResultList();
 return result;
 }




When I look at the db table I can see that revisions are being added to the table when I add the address to the set. But when I inspect the list then PersonalContact entity does not contain the addresses I entered.

I have declared @Versioned at Contact superclass level and declared at the subclasses as well (which I presume I don't need to do).

Hopefully this should be my last post!

  • 1. Re: Inheritence
    Adam Warski Master

    Hello,

    which inhertinace type do you use? Currently, only single-table inheritance will work. For support on the others, watch the progress on http://opensource.atlassian.com/projects/hibernate/browse/HHH-3564.

    --
    Adam

  • 2. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    I am using single-table inheritence at the moment

    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    


    Still can't see the address that I've added.




  • 3. Re: Inheritence
    Adam Warski Master

    Can you please post your entities?

    --
    Adam

  • 4. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    Contact.java

    @Entity
    @org.hibernate.annotations.Entity(
     selectBeforeUpdate = true,
     dynamicInsert = true, dynamicUpdate = true)
    @Table(name="T_CONTACT")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorValue("Contact")
    @DiscriminatorColumn(name="contactType",discriminatorType=javax.persistence.DiscriminatorType.STRING)
    @Versioned
    @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
    public class Contact implements Serializable {
    
     @SuppressWarnings("unused")
     private static Logger logger = Logger.getLogger(Contact.class);
    
     private static final long serialVersionUID = 1L;
    
     @Id @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="C_CONTACT_ID")
     @Type(type="java.lang.Long")
     @DocumentId
     private Long id;
    
     @Column(name="C_EMAIL")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String email;
    
     @Column(name="C_CREATEDON")
     @Type(type="java.util.Date")
     private Date createdOn;
    
     @Column(name="C_LASTUPDATEDON")
     @Type(type="java.util.Date")
     private Date lastUpdatedOn;
    
     @OneToMany(mappedBy="contact",targetEntity=com.amin.gigaspaces.common.domain.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
     @IndexedEmbedded
     @JoinColumn(name="C_CONTACT_ID")
     @Type(type="java.util.Set")
     @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
     private Set<Address> addresses;
    
     @OneToMany(mappedBy="contact", targetEntity=com.amin.gigaspaces.common.domain.Phone.class,cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
     @IndexedEmbedded
     @JoinColumn(name="C_CONTACT_ID")
     @Type(type="java.util.Set")
     @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
     private Set<Phone> phoneNumbers;
    
     @OneToMany(mappedBy="contact", targetEntity=com.amin.gigaspaces.common.domain.Note.class,cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
     @IndexedEmbedded
     @JoinColumn(name="C_CONTACT_ID")
     @Type(type="java.util.Set")
     @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
     private Set<Note> contactNotes;
    
     @Column(name="C_NOTES")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String notes;
    
     @Column(name="C_TRASHED")
     private boolean trashed;
    
     ...getters and setters
    


    PersonalContact.java
    @Entity
    @DiscriminatorValue("PersonalContact")
    @SuppressWarnings("serial")
    @Versioned
    @Indexed
    public class PersonalContact extends Contact {
    
     @Column(name="P_FIRSTNAME")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String firstname;
    
     @Column(name="P_SURNAME")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String surname;
    
     @Column(name="P_DATEOFBIRTH")
     @Type(type="java.util.Date")
     private Date dateOfBirth;
    
     @Column(name="P_NOTIFYBIRTHDAY")
     @Type(type="boolean")
     private boolean notifyBirthDay;
    
     @Column(name="P_MYFACESURL")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String myFacesUrl;
    
     @Column(name="P_REMINDERCOUNT")
     private int reminderCount;
    
     @Column(name="P_REMINDERRESET")
     @Type(type="boolean")
     private boolean reset;
    
    ...getters and setters
    
    


    Address.java
    @Entity
    @org.hibernate.annotations.Entity(
     selectBeforeUpdate = true,
     dynamicInsert = true, dynamicUpdate = true)
    @Indexed
    @Table(name="T_ADDRESS")
    @Versioned
    @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
    public class Address implements Serializable {
     private static final long serialVersionUID = 1L;
    
    
     @Id @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="A_ADDRESS_ID")
     @Type(type="java.lang.Long")
     @DocumentId
     private Long id;
    
     @Column(name="A_ADDRESS1")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String address1;
    
     @Column(name="A_ADDRESS2")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String address2;
    
     @Column(name="A_TOWN")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String town;
    
     @Column(name="A_COUNTY")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String county;
    
     @Column(name="A_COUNTRY")
     @Field(index=Index.TOKENIZED, store=Store.YES)
    
     private String country;
    
     @Column(name="A_POSTCODE")
     @Field(index=Index.TOKENIZED, store=Store.YES)
     private String postcode;
    
     @Column(name="A_ACTIVE")
     @Type(type="boolean")
     private boolean active;
    
     @Column(name="A_CREATEDON")
     @Type(type="java.util.Date")
     private Date createdOn;
    
     @Column(name="A_LASTUPDATEDON")
     @Type(type="java.util.Date")
     private Date lastUpdatedOn;
    
     @ContainedIn
     @ManyToOne
     @JoinColumn(name="C_CONTACT_ID", nullable=false, insertable=true, updatable=true)
     private Contact contact;
    
    ...getters and setters
    


    Hope that helps.

  • 5. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    Hi

    I was looking at the other posts and noticed someone used


    @VersionsJoinTable
    


    Do I need to use this for my OneToMany collection?

  • 6. Re: Inheritence
    Adam Warski Master

    Hello,

    no, @VersionsJoinTable is optional and only useful for unidirectional @OneToMany relations.

    Could you maybe create a test case and post a JIRA bug?

    --
    Thanks,
    Adam

  • 7. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    Sure. I'm just wondering if it;s something I am doing wrong rather than it being a bug.

    Here is what I am doing:

    1) Create a personal contact with no address (email = test@test.com)

    2) Update personal contact email address (email = test2@test.com)

    At this point I get back one entity in the list.

    3) I add address to personal contact

    At this stage I have an entry in the revisions table for personal contact with contactType being Contact rather than personal contact

    4) Add another address to personal contact

    When I inspect the entities in the collection they don't have any address attached to them. When I added the 2nd address should I not see the first address in the personal contact set when I inspect the personal contact history?

    As I mentioned I am happy to raise a JIRA but I'm not sure if this is a bug...

  • 8. Re: Inheritence
    Adam Warski Master

    Hello,

    it sounds like it may be a bug :) The adressess should of course be in the history of the contact. If they appear in the "real" entities, they should also appear in the history :).

    --
    Adam

  • 9. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    Hi

    I will raise a JIRA issue, I could not find Envers project when creating a JIRA using the below url:
    http://opensource.atlassian.com/projects/hibernate/secure/CreateIssue!default.jspa

    Can I put under another project?

    Thanks

  • 10. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    Got it...sorry I know where to put it. Just had another daughter so not getting enough sleep!

  • 11. Re: Inheritence
    Adam Warski Master

    Yeah, just use the envers component.

    Congratulations! :)
    Adam

  • 13. Re: Inheritence
    Amin Mohammed-Coleman Newbie

    I'm using Spring and Hibernate together, is it ok to add the Spring testcase to the JIRA?

  • 14. Re: Inheritence
    Adam Warski Master

    Hello,

    I've identified the bug so now it's just a matter of solving it ;)

    --
    Adam

1 2 Previous Next