Reputation: 18864
Is it possible to change the target table for a Hibernate mapping? My use case is that while I get the data from one table, in cases where the data cannot be processed it is stored in a error table for later analysis.
Although it is possible to define entity-names in hibernate mappings, in my opinion it is not appropriate because it requires duplication of mappings. The same mapping would then exist for getting the data and a copy of it with a different entity-name for the error table.
Do you have any ideas how to approach and solve this problem?
Upvotes: 1
Views: 3082
Reputation: 7739
Is it possible for you to use a superclass? If the data has all of the same mapping information, but belongs to a different table, then you could place the mappings on a common parent class of both entities. Use a mapped superclass to define the common properties, then define each concrete class as a different entity, inherity the properties. In hibernate annotations, this is accomplished using the @MappedSuperclass annotation on the parent.
Upvotes: 0
Reputation: 33775
As said,
Where the data cannot be processed it is stored in a error table for later analysis
Besides ChssPly76's answer, you can define a global HibernateException handler (if you use Spring to wrapper hibernate connections, you should use DataAccessException) and log errors details in your database or other devices such as a web page where you can see it in any place.
regards,
Upvotes: 0
Reputation: 403451
As @ChssPly76 mentioned, you can jump through Hibernate's API hoops to get the raw configuration, fiddle them, and build yourself a new SessionFactory
.
Another equally unpleasant solution would be to use XSLT to take your mapping config file and swap out the table name for something else, then feed that modified file into hibernate to build a new SessionFactory
.
It's quite distatseful, but it does mean you can avoid getting mired in the depths of Hibernate.
Upvotes: 2
Reputation: 100706
Short answer - no. Defining a separate mapping (via entity name) is the way to go.
Long answer - it's possible via runtime manipulation of Configuration prior to SessionFactory being built. You can get the mapping for the entity in question from Mappings as defined in Configuration, copy it under new entity name and associate it with a copy of the underlying Table under the new name. You would in effect still be defining a separate mapping, but doing it the hard way.
Take a look at this question.
Upvotes: 5