Hi all!
I have a question, something I have a really hard time finding any docs/example cases for:
I am working with a non-changeable PostgreSQL DB, and creating a pretty straightforward (Swing) App to edit manipulate data in it.
What makes it less straightforward is that to read certain Entities I am supposed to use a Database Function (like a prepared statement) that return EXACTLY the same columns as if I would read directly from the table that function is accessing (I still need to use the function, because things get filtered through the function).
But obviously, when the app should write back some Entites altered by the user (front end) I need to write them to the original table they are coming from.
So when I define the Entity in java I can do this, I can read and write to the table "report_entity_archive":
@Entity @Table(name="report_entity_archive") public class ReportEntity extends BaseEntity {...}
But because I need to read from a function, I also need this
@Entity @Table(name="awv.get_report_companies('T')") public class FilteredReportEntity extends ReportEntity { //EMPTY, I just need this to point to the Database function, but has the same fields like ReportEntity }
Reading from the function works fine, in a way, but of course I cant persist with this class, because you cant update/insert to a database function (sql syntax error).
Which I thought is fine, I thought I can just query the objects using the "FilteredReportEntity.class" and then downcast it to "ReportEntity" and store that with .save(..).
But Hibernate starts behaving really weirdly, as soon as I add "FilteredReportEntity" to the mapped classes Hibernate will query for them twice, even though I only call .list(..) once. It will select all from the table used for the ReportEntity, but then also query all from the db-function used in FilteredReportEntity and I have all in my list.
And also, if I want to persist the ReportEntity (by "downcasting" FilteredReportEntity to ReportEntity) it will still try to persist to the DB function rather than the table.
Does that mean I need two redundant classes with exactly the same fields, and manual field copying between them, just to be able to read from a db function, but write to a normal table (remember, both have exactly(!!) the same result fields).
I hope this made sense, any help/thoughts/critisism is appreciated!!
Cheers!! CC!
|