@utluiz, got grade 10! I had to do the following, I created a view in my BD:
select c.id_customer, c.tenant_id, c.first_name, ds.name
from viatgedb.customer c
join viatgedb.customer_service cs
on c.id_customer = cs.fk_customer
join viatgedb.destination_requested ds on cs.id_customer_service = ds.fk_customer_service;
Soon after I mapped JPA (I’m working with multi-tenant architecture):
@ReadOnly
@Entity
@Table(name="vw_open_services")
@Multitenant
@TenantDiscriminatorColumn(name = "tenant_id", discriminatorType = DiscriminatorType.INTEGER, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
public class VwOpenService implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="id_customer")
private Long id;
@Column(name="tenant_id", insertable=false, updatable=false)
private Long tenantId;
@Column(name="day_service")
private String dayService;
@Column(name="first_name")
private String firstName;
@Column(name="NAME")
private String name;
// Getter and Setter
And I usually create a repository to bring my records with Spring Data:
@Repository
public interface OpenServiceRepository extends BaseRepository<VwOpenService, Long>{
List<VwOpenService> findAll();
}
The result was as expected.
Thank you
Salve @utluiz! I am reading here about the creation of it. I will try to implement, but how is it next to Spring Data? i realize a normal select? In what I could understand I think it would be like this.
– João Manolo
@Joãomanolo It would be exactly the same as a normal entity. You can use JPQL or Criteria the same way.
– utluiz
there is a problem declaring the annotation
@Entity
. She Ped me a@id
as far as I know it doesn’t make much sense for a view to contain a primary key. How did you treat it?– João Manolo
@Joãomanolo Yes, there is this limitation on the part of the JPA API. There must be some field in your view that is unique. Note it as
@Id
fearless.– utluiz