• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about Hibernate Inheritance

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to solve a problem that has probably been answered more than once, but for the life of me, I cannot find the solution anywhere.

I have a persistent class called item which looks something like what is listed below (though I've left out many of the details for brevity):

@Entity
@Table(name="ITEMS")
public class Item {
@Id
@Column(name="id")
@GeneratedValue
private int id;

@Column("name")
private String name;

...
}

I have no issues doing any data manipulation of this class via the standard Hibernate methods.

In a JSF application, I'm going to present a list of Item objects and allow a user to select ones to delete using a checkbox (pretty standard stuff here ).

I want to USE the Item class since it exposes everything I need, with the exception of a boolean flag called "selected." I therefore created a new class as follows (and again, I've left out getters/setters):

public class SelectableItem extends Item {
public boolean isSelected;

...
}


Everything is great until I go to save the SelectableItem class back to the database because Hibernate (naturally) does not have a mapping from SelectableItem into the database. I don't want to put the flag in the base class because I want to keep the Item class separate from the UI (obviously). I also don't want to use composition and then have to expose ALL of the enclosed class's methods. Is there a simple, more elegant, solution to this problem or is what I'm trying to do here "half-baked?"

I hope my explanation was accurate enough and thanks, in advance for any help provided.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you are using the single-parameter save() method, right? Something like:



That method is overloaded. Maybe you could try using the other version, with the entityName parameter. Like:



I haven't used it, so I'm not giving any guarantee, but I have a feeling it might help.
 
Dave Bixler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, that pretty much solved the problem. It was for delete, so the end code was:

public void deleteItem (Item i) {
...
sess.delete (Item.class.getName (), i);
...
}

Thank you very much for the quick reply!
 
I am mighty! And this is a mighty small ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic