Set table name in SpringData Jpa Repository instead of in entity - java

I have a JPA entity, which I want to store exactly with the same fields in two different tables for different purposes.
I assumed, that there is a possibility to set which table to actually store data in Spring Data Jpa repository, but couldn't find how to do it.
Is it possible?
If not, what is the best way to achieve my goal with spring data jpa and without copying the entity class?

You may want to define an abstract class for your entity using #MappedSuperclass annotation. You can specify your field mappings in this class. Then have two separate entity classes which inherits your abstract class and each of those two can map to their own table.

Related

Spring boot, repository without entity

Is it possible to create a repository without an entity?I've been working on a project and I need to take data from different tables. So I can't create an entity because I don't have that kind of table in DB.
How can I do that, Please Help?
You need an aggregator. If you know exactly which data you do need to work with, you can create a view with columns from those different tables and define an entity class with those column names in the view. Hence you can implement the repository just as the regular way with that entity of aggregated view.
But, if you have to fetch and combine multiple entities and you actually don't know how much varieties are there in fetching data and entities, you can write a dao as service or component and access different entities through different repositories and then aggregate the data programmatically as your need. You can use native queries too to access various data from different tables.

how to specify queries for CrudRepository methods in spring data repositories?

I am planning to use PagingAndSortingRepository interface methods and wanted to understand how do we specify what query to run on calling the method such as findAll?
I'm assuming JPA, but other stores work the same or in case of annotating the entity model similar.
Normally you don't. Spring Data figures out these queries on your behalf. If you want a different query to be executed than the one Spring Data picks, you have the usual choices for custom methods in Spring Data:
If you only need to change table or column names you can annotate your entity model.
provide a #Query annotation.
provide a complete custom annotation.

Linking entities to existing tables without code

Let's assume that we have java entities already implemented and annotated with Jpa annotations.
We also have an existing database slightly different to the schema described by said entities.
How I can link the data base with my entities without the code?
Otherwise, how can i proceed from the begining when implementing my entities to make this stuff configurable ( give the user the possiblity of specifying the names of the columns corresponding to the fields of each entity in an externalized configuration file)?
NB: I use hibernate as an ORM.
I believe this is what you are looking for

Mapping Multiple tables to Single Entity

I have a Database that contains multiple tables which are generated on a per month basis. e.g
Transaction_01_2014
Transaction_02_2014
Transaction_03_2014
.
.
.
Transaction_12_2014
All the tables have the same structure. The only difference is the month and year appended at the end.
Is it possible to map all these tables to a single #Entity class "Transaction" in Hibernate? if yes, do I need to change some configurations? If its not possible this way, how should I operate on these tables in an subtle and simple manner, from my java application?
Cheers!
Yes. You can map multiple tables to a single #Entity class using #MappedSuperclass annotation. By creating a super with this annotation & inheriting it's feature to all sub-classes with different tables in #Table annotation. But, in this case problem of normalization arises.
OR
You can use #SecondaryTable annotation which allows you to specify one or more secondary tables indicating that the data for the entity class is stored across multiple tables.
For more information :
#MappedSuperclass - Java Doc Example
#SecondaryTable - Java Doc Example
Tutorial - Map One POJO To Multiple Tables
If the purpose of this new entity is to read and not for insert, update and delete, like reporting and monitoring then following the approach may help you:
You may create a database VIEW that unions-all these tables.
Having the new #Entity class mapped to these VIEW will solve the problem.
The View's definition(DDL) needs to be updated by a database job
(side by side of tables generation per month )

Should I use a facade to access JPA Entities?

I want to retrieve a product from my database. A product is made up of data from several different tables.
Is it best to create a facade that will talk to multiple entities in order to pull together all the info needed to make a product and should the facade marshal the object or is there another way to do this?
I was thinking the method in the facade would be getProduct() and this method would use multiple entities?
Thanks
Why not use a product Entity and have JPA do it all for you? An entity can span multiple tables, and your product Entity can itself have references to other entities where it makes sense. Your application can make accessors within the Product class to expose the referenced entities or just return data from those references as needed.

Categories