JPA JOIN without cross joins - jpa

I'm trying to have a relationship between the following tables
COMBINE
#Entity
#NamedQuery(name="Combine.findAll", query="SELECT c FROM Combine c")
#Table(name="COMBINE")
public class Combine implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private int id;
private int artistid;
private int automated;
private int eventid;
private int locationid;
#Column(name="main_artist")
private int mainArtist;
public Combine() {
}
Event
#Entity
#NamedQuery(name="Event.findAll", query="SELECT e FROM Event e")
#Table(name="event")
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private int id;
private boolean active;
private String addbyspider;
private int automated;
#Lob
private String description;
private Timestamp doorsopen;
private Timestamp endtime;
private Timestamp lastfm;
#Column(name="latest_update")
private Timestamp latestUpdate;
private int minimumageid;
private String name;
private Timestamp nolatertime;
private int podiuminfo;
#Column(name="short_description")
private String shortDescription;
#Column(name="sold_out")
private int soldOut;
private Timestamp time;
#Column(name="update_by_bot")
private Timestamp updateByBot;
/* TO DO: LINK NAAR EVENTPRICE */
// public EventPrice getEventPrice() {
// return eventPrice;
// }
//
// public void setEventPrice(EventPrice eventPrice) {
// this.eventPrice = eventPrice;
// }
//
// #OneToOne(mappedBy = "event_price")
// private EventPrice eventPrice;
//bi-directional many-to-many association to Artist
//#ManyToMany(fetch = FetchType.LAZY)
#ManyToMany
#JoinTable(
name="COMBINE"
, joinColumns={
#JoinColumn(name="eventid")
}
, inverseJoinColumns={
#JoinColumn(name="artistid")
}
)
private List<Artist> artists;
#OneToOne
#JoinColumn(name="eventid")
private List<Combine> combine;
//bi-directional many-to-many association to Location
//#ManyToMany(fetch = FetchType.EAGER)
#OneToMany
#JoinTable(
name="COMBINE"
, joinColumns={
#JoinColumn(name="eventid")
}
, inverseJoinColumns={
#JoinColumn(name="locationid")
}
)
private List<Location> locations;
Location
#Entity
#NamedQuery(name="Location.findAll", query="SELECT l FROM Location l")
#Table(name="location")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private int id;
private boolean active;
#Column(name="at_owner")
private int atOwner;
#Column(name="bad_ticket_site")
private int badTicketSite;
#Lob
private String description;
private int expectedBotItems;
private int expectedBotRuntime;
#Column(name="last_run")
private Timestamp lastRun;
private String name;
private boolean notPrimaryBot;
private int organisationid;
private short priorityid;
#Column(name="short_description")
private String shortDescription;
#Column(name="spider_by")
private int spiderBy;
What this currently does is generate cross joins with the tables with the following TypedQuery
StringBuffer sb = new StringBuffer();
sb.append("SELECT e FROM Event e, Combine c, Location l ");
sb.append("where e.id = c.eventid and l.id = c.locationid ");
sb.append("and e.time>=NOW() and e.soldOut='0' and e.active>=1");
This generates
select event0_.id as id1_26_,
event0_.active as active2_26_,
event0_.addbyspider as addbyspi3_26_,
event0_.automated as automate4_26_,
event0_.description as descript5_26_,
event0_.doorsopen as doorsope6_26_,
event0_.endtime as endtime7_26_,
event0_.lastfm as lastfm8_26_,
event0_.latest_update as latest_u9_26_,
event0_.minimumageid as minimum10_26_,
event0_.name as name11_26_,
event0_.nolatertime as nolater12_26_,
event0_.podiuminfo as podiumi13_26_,
event0_.short_description as short_d14_26_,
event0_.sold_out as sold_ou15_26_,
event0_.time as time16_26_,
event0_.update_by_bot as update_17_26_
from
event event0_
cross join COMBINE combine1_
cross join location location2_
where event0_.id=combine1_.eventid and location2_.id=combine1_.locationid and event0_.time>=now() and event0_.sold_out='0' and event0_.active>=1 limit ?
I do not want to create CROSS JOINS at all but instead I would like INNER JOIN between the tables. How could I achieve this?
There is something wrong with the OneToOne as it doesn't build to the server. Anyone know why this doesn't work?
#OneToOne
#JoinColumn(name="eventid")
private List<Combine> combine;

Related

Spring Boot : Building object Rest API

I created 4 classes for 3 tables. Those tables are built to store orders with products.
My first entity is orders :
#Entity
#Table(name="orders")
public class OrderEntity implements Serializable {
...
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
private UserEntity seller;
private String paymentMode;
private double totalAmount;
#OneToMany(mappedBy = "pk.order")
private List<OrderProductEntity> orderProducts;
#DateTimeFormat
private Date createdAt;
...
}
My second entity is products:
#Entity
#Table(name="products")
#Getter #Setter
public class ProductEntity implements Serializable {
...
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
#Column(nullable = false)
private String productKeyId;
#ManyToOne
#JoinColumn(name = "category_id")
private CategoryEntity category;
#Column(nullable = false)
private String name;
#Column(nullable = false)
private double price;
#Column(nullable = false)
private int availableQty;
#JsonManagedReference
#OneToMany(mappedBy = "pk.product", fetch = FetchType.EAGER)
#Valid
private List<OrderProductEntity> orderProducts;
...
}
I also define the pivot table in the OrderProductEntity:
#Entity
#Table(name="order_product")
#Getter #Setter
public class OrderProductEntity {
#EmbeddedId
#JsonIgnore
private OrderProductPK pk;
#Column(nullable = false)
private Integer quantity;
// default constructor
public OrderProductEntity(){}
public OrderProductEntity(OrderEntity order, ProductEntity product, Integer quantity) {
pk = new OrderProductPK();
pk.setOrder(order);
pk.setProduct(product);
this.quantity = quantity;
}
}
And I defined the PK of this table :
#Embeddable
#Getter #Setter
public class OrderProductPK implements Serializable {
#JsonBackReference
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#JoinColumn(name = "order_id")
private OrderEntity order;
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#JoinColumn(name = "product_id")
private ProductEntity product;
}
Now my issue is to return the data to the front office by a web service Rest.
I created a model that represent what i want to obtain as result.
public class OrderRest {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRest> orderProducts;
private UserRest seller;
private Date createdAt;
}
And :
public class ProductRest {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRest category;
}
In my controller i want to return an OrderRest object :
public List<OrderRest> getOrders() {
List<OrderRest> returnValue = new ArrayList<>();
List<OrderDto> orderDtos = orderService.getOrders();
// loop the result
for (OrderDto orderDto : orderDtos) {
OrderRest orderRest = new OrderRest();
ModelMapper modelMapper = new ModelMapper();
orderRest = modelMapper.map(orderDto, OrderRest.class);
returnValue.add(orderRest);
}
return returnValue;
}
THis method map the List of OrderRest and call the service layer :
public List<OrderDto> getOrders() {
List<OrderDto> returnValue = new ArrayList<>();
Iterable<OrderEntity> orderEntities = orderRepository.findAll();
ModelMapper modelMapper = new ModelMapper();
for(OrderEntity orderEntity: orderEntities) {
OrderDto orderDto = new OrderDto();
UserDto userDto = orderDto.getSeller();
List<OrderProductDto> orderProductDtos = orderDto.getOrderProducts();
orderDto = modelMapper.map(orderEntity, OrderDto.class);
returnValue.add(orderDto);
}
return returnValue;
}
With OrderDto :
public class OrderDto implements Serializable {
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<OrderProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
And OrderProductDto :
public class OrderProductDto implements Serializable {
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private String productKeyId;
private String name;
private Integer price;
private Integer qty;
private String imgPath;
private CategoryDto category;
}
My issue is concerning what i return from the REST API :
my product list returns nothing :
"orderProducts": [
{
"productKeyId": null,
"name": null,
"price": 0.0,
"qty": 0,
"imgPath": null,
"category": null
},...
]

org.springframework.dao.InvalidDataAccessResourceUsageException in spring data jpa

i have (one to many) and (many to one) bi-directional mappings while i am querying with #Query with spring- data-jpa using repository using foreign key iam getting InvalidDataAccessApiUsageException
#Entity(name="docket")
#JsonIgnoreProperties(ignoreUnknown = true)
public class Docket implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
#Column(name="docket_Id")
private long Id;
#Column(name="docket_name")
private String docket_name;
#Column(name="area_in_kms")
private long area_in_kms;
#Column(name="number_of_connections")
private long number_of_connections;
#Column(name="number_of_sewer_connections")
private long number_of_sewer_connections;
#Column(name="number_of_manholes")
private long number_of_manholes;
#Column(name="deep_manholes")
private long deep_manholes;
#Column(name="contour_low")
private long contour_low;
#Column(name="contour_high")
private long contour_high;
#Column(name="length_of_sewermain_kms")
private long length_sewer_in_kms;
#JsonBackReference(value="section-details")
#ManyToOne(cascade= CascadeType.ALL,fetch = FetchType.EAGER)
#JoinColumn(name = "section_Id")
private Section section;
#Entity(name="section")
#JsonIgnoreProperties(ignoreUnknown = true)
public class Section implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
#Column(name="section_Id")
private long Id;
#Column(name="section_name")
private String section_name;
#Column(name="area_in_kms")
private long area_in_kms;
#Column(name="calculated_length")
private long calculated_length;
#Column(name="area_geometry")
private long area_geometry;
//#JsonManagedReference
#OneToMany(mappedBy = "section", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Set<Docket> docket;
#Repository
#Transactional(readOnly = true)
public interface DocketRepository extends JpaRepository<Docket, Long> {
#Modifying(clearAutomatically = true)
#Query(nativeQuery=true,value= "select d from docket d where d.section_id = :section_id" )
List<Docket> findAlldocketsforsection(#Param("section_id") long id);
need help!! on this i need to query more custom queries in this api please suggest me from where i am getting error

Jpa Auditing dont save data in table auditing

I have to implementes Auditing in my aplication.. i inserting this data correctly
but i want to save all atributter from my Entity ,
Exemple, name, epigrafe, .. and olthers.
I implemented the mothod but dosent work, just dont save the atributte..
lets see..
#Entity
#EntityListeners(AuditingEntityListener.class)
#Table(name = "logradouros_historico", schema = "aud")
public class LogradourosHistorico {
#Id
#GeneratedValue
private Long id;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "id_logradouro")
private Logradouros logradouro;
#CreatedBy
private String modificadoPor;
#CreatedDate
#Temporal(TemporalType.TIMESTAMP)
private Date modifiedDate = new Date();
#Enumerated(EnumType.STRING)
private Acoes acao;
#Column(name = "nome")
private String nome; //nome do logradouro
public LogradourosHistorico() {
super();
}
public LogradourosHistorico(Logradouros logradouro, String modificadoPor,
Acoes acao) {
super();
this.logradouro = logradouro;
this.modificadoPor = modificadoPor;
this.acao = acao;
}
//getters and setters
my class entityListner
public class LogradourosEntityListener {
#PostPersist
public void prePersist(Logradouros target) {
perform(target, Acoes.INSERTED);
}
#PreUpdate
public void preUpdate(Logradouros target) {
perform(target, Acoes.UPDATED);
}
#PreRemove
public void preRemove(Logradouros target) {
perform(target, Acoes.DELETED);
}
#Transactional()
private void perform(Logradouros target, Acoes acao) {
target.getNome();
EntityManager entityManager = BeanUtil.getBean(EntityManager.class);
entityManager.persist(new LogradourosHistorico(target, acao));
}
}
my class Logradouros
#Entity
#EntityListeners(LogradourosEntityListener.class)
#Table(name = "logradouros", schema = "glb", uniqueConstraints= #UniqueConstraint(columnNames={"id_entidade", "idLogradouro"}))
public class Logradouros extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 3703309412387185484L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idLogradouro;
#Column(name = "cep_geral")
private String cepGeral;
#Column(name = "epigrafe")
private String epigrafe;
#NotNull
#Column(name = "nome")
private String nome;
#Column(name = "nome_exibicao")
private String nomeExibicao;
#JoinColumn(name = "id_entidade")
#ManyToOne(/*cascade = CascadeType.ALL*/)
private Entidades entidade;
#NotNull
#JoinColumn(name = "id_municipio")
#ManyToOne(/*cascade = CascadeType.ALL*/)
private Municipios municipio;
// gettrs and settrs
so what i did wrong because i cant get the nome of entity Logradouros

Spring Data JPA Specification Manyto One relationship

I have two entities in Spring Data JPA:
User--->* TaxPayment
The goal is to get the all the taxpayments related to user_id:
User.java
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
..........
}
TaxPayment.jva
public class TaxPayment implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Column(name = "payment_date", nullable = false)
private LocalDate paymentDate;
#NotNull
#Column(name = "amount", precision=10, scale=2, nullable = false)
private BigDecimal amount;
#Column(name = "reference")
private String reference;
#ManyToOne
private User user;
public Long getId() {
return id;
}..
}
I dont wan't to have oneTOMany Annotation from User.java and the column mapped in the Taxpayment as user_id.
Specification class as follows:
final class TaxPaymentSpecification {
private TaxPaymentSpecification(){
}
static Specification<TaxPayment> hasUser(Long userId){
return new Specification<TaxPayment>() {
#Override
public Predicate toPredicate(Root<TaxPayment> arg0, CriteriaQuery<?> arg1,
CriteriaBuilder arg2) {
// TODO Auto-generated method stub
Root<TaxPayment> root = arg0;
Subquery<Long> subqry = arg1.subquery(Long.class);
Root<User> user = subqry.from(User.class);
final Join<User,TaxPayment> taxpays = root.join("user");
subqry.select(taxpays.<Long> get("user_id"));
subqry.where(arg2.equal(user.<Long> get("id"),userId));
return arg2.in(arg0.get("user_id")).value(subqry);
}
};
}
}
Is specification is correct or wrong as per my goal to get all the TaxPayment related to user_id?
There is no need to use subquery if you already know the userId, which will be the value of user_id in TaxPayment table:
#Override
public Predicate toPredicate(Root<TaxPayment> arg0, CriteriaQuery<?> arg1,
CriteriaBuilder arg2) {
return arg2.equal(arg0.get("user_id"), userId);
}

JPA query with OneToOne lazy

I have this structure of object
#Entity
public class Member {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long memberId;
private String name;
private boolean man;
private String address;
#OneToOne(fetch = FetchType.LAZY)
private City city;
private String postalCode;
private String phone1;
private String phone2;
private LocalDate birthdate;
private String email;
private String emergencyContactName;
private String emergencyPhone;
private String paymentGatewayKey;
#OneToMany(fetch = FetchType.LAZY)
private List<Contract> contracs;
#OneToOne(fetch = FetchType.LAZY)
private Commerce commerce;
}
#Entity
public class Contract {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long contractId;
private BigDecimal price;
private int frequency;
private int term;
private LocalDate startDate;
private LocalDate endDate;
private int numberOfPayment;
#Enumerated(EnumType.STRING)
private StatusEnum status;
#OneToMany(fetch = FetchType.LAZY,mappedBy = "contract")
private List<Payment> payments;
#ManyToOne
private Member member;
}
#Entity
public class Payment {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long paymentId;
private BigDecimal price;
private LocalDate date;
#Enumerated(EnumType.STRING)
private StatusEnum status;
#Enumerated(EnumType.STRING)
private PaymentModeEnum paymentMode;
#ManyToOne
private Contract contract;
#OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Operation> operations;
}
is it possible from a member query to get only the needed contract, payment, city and commerce info?
If member have many contract... i want to get only contract #2...
I started this query but city and commerce are lazy and i don't know what to do with theses fields.
select m from Member m inner join fetch m.contracs c inner join fetch c.payments p where c.contractId = :contractId