I have problem with my application. Currently I have Vehicles and Order table having many to many relationship. The problem is when I get input from front-end, the staging table for order_vehicle does not update. I am not sure how the many to many works in SpringBoot and there is annotation I have added such as #JsonIgnore which if not added I will have infinite recursive JSON. I keep trying to solve my problem but couldn't. I am using Hibernate 5.2 and do reverse engineering from my database.
Order.java
package com.jdm.entity;
// Generated 11-Dec-2019 10:05:44 by Hibernate Tools 5.2.12.Final
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
/**
* Order generated by hbm2java
*/
#Entity
#Table(name = "order", catalog = "crud")
public class Order implements java.io.Serializable {
private OrderId id;
private Date date;
private Float total;
#JsonIgnore
private Customer customer;
#JsonIgnore
private Employee employee;
//#JsonIgnore
private Set<Vehicle> vehicles = new HashSet<Vehicle>(0);
public Order() {
}
public Order(OrderId id, Customer customer, Employee employee) {
this.id = id;
this.customer = customer;
this.employee = employee;
}
public Order(OrderId id, Customer customer, Employee employee, Date date, Float total, Set<Vehicle> vehicles) {
this.id = id;
this.customer = customer;
this.employee = employee;
this.date = date;
this.total = total;
this.vehicles = vehicles;
}
#EmbeddedId
#AttributeOverrides({ #AttributeOverride(name = "OId", column = #Column(name = "o_id", nullable = false)),
#AttributeOverride(name = "customerCustId", column = #Column(name = "customer_cust_id", nullable = false)),
#AttributeOverride(name = "employeeEmpId", column = #Column(name = "employee_emp_id", nullable = false)) })
public OrderId getId() {
return this.id;
}
public void setId(OrderId id) {
this.id = id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "customer_cust_id", nullable = false, insertable = false, updatable = false)
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "employee_emp_id", nullable = false, insertable = false, updatable = false)
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
#Temporal(TemporalType.DATE)
#Column(name = "date", length = 10)
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
#Column(name = "total", precision = 12, scale = 0)
public Float getTotal() {
return this.total;
}
public void setTotal(Float total) {
this.total = total;
}
#ManyToMany(fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
// #JsonBackReference
#JoinTable(name = "order_has_vehicle", catalog = "crud", joinColumns = {
#JoinColumn(name = "order_o_id", nullable = false, updatable = false),
#JoinColumn(name = "order_customer_cust_id", nullable = false, updatable = false),
#JoinColumn(name = "order_employee_emp_id", nullable = false, updatable = false) }, inverseJoinColumns = {
#JoinColumn(name = "vehicle_v_id", nullable = false, updatable = false) })
public Set<Vehicle> getVehicles() {
return this.vehicles;
}
public void setVehicles(Set<Vehicle> vehicles) {
this.vehicles = vehicles;
}
}
OrderId.java
package com.jdm.entity;
// Generated 16-Dec-2019 23:11:42 by Hibernate Tools 5.2.12.Final
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* OrderId generated by hbm2java
*/
#Embeddable
public class OrderId implements java.io.Serializable {
private int OId;
private int customerCustId;
private int employeeEmpId;
public OrderId() {
}
public OrderId(int OId, int customerCustId, int employeeEmpId) {
this.OId = OId;
this.customerCustId = customerCustId;
this.employeeEmpId = employeeEmpId;
}
#Column(name = "o_id", nullable = false)
public int getOId() {
return this.OId;
}
public void setOId(int OId) {
this.OId = OId;
}
#Column(name = "customer_cust_id", nullable = false)
public int getCustomerCustId() {
return this.customerCustId;
}
public void setCustomerCustId(int customerCustId) {
this.customerCustId = customerCustId;
}
#Column(name = "employee_emp_id", nullable = false)
public int getEmployeeEmpId() {
return this.employeeEmpId;
}
public void setEmployeeEmpId(int employeeEmpId) {
this.employeeEmpId = employeeEmpId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof OrderId))
return false;
OrderId castOther = (OrderId) other;
return (this.getOId() == castOther.getOId()) && (this.getCustomerCustId() == castOther.getCustomerCustId())
&& (this.getEmployeeEmpId() == castOther.getEmployeeEmpId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getOId();
result = 37 * result + this.getCustomerCustId();
result = 37 * result + this.getEmployeeEmpId();
return result;
}
}
Vehicle.java
package com.jdm.entity;
// Generated 11-Dec-2019 10:05:44 by Hibernate Tools 5.2.12.Final
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Vehicle generated by hbm2java
*/
#Entity
#Table(name = "vehicle", catalog = "crud")
public class Vehicle implements java.io.Serializable {
private Integer VId;
private String VManufacturer;
private String VModel;
private Integer VYear;
private String VCategory;
private String VTransmission;
private Integer VMilleage;
private String VPlate;
private String VColor;
private Float VPrice;
private Set<Order> orders = new HashSet<Order>(0);
public Vehicle() {
}
public Vehicle(String VManufacturer, String VModel, Integer VYear, String VCategory, String VTransmission,
Integer VMilleage, String VPlate, String VColor, Float VPrice, Set<Order> orders) {
this.VManufacturer = VManufacturer;
this.VModel = VModel;
this.VYear = VYear;
this.VCategory = VCategory;
this.VTransmission = VTransmission;
this.VMilleage = VMilleage;
this.VPlate = VPlate;
this.VColor = VColor;
this.VPrice = VPrice;
this.orders = orders;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "v_id", unique = true, nullable = false)
public Integer getVId() {
return this.VId;
}
public void setVId(Integer VId) {
this.VId = VId;
}
#Column(name = "v_manufacturer", length = 45)
public String getVManufacturer() {
return this.VManufacturer;
}
public void setVManufacturer(String VManufacturer) {
this.VManufacturer = VManufacturer;
}
#Column(name = "v_model", length = 45)
public String getVModel() {
return this.VModel;
}
public void setVModel(String VModel) {
this.VModel = VModel;
}
#Column(name = "v_year")
public Integer getVYear() {
return this.VYear;
}
public void setVYear(Integer VYear) {
this.VYear = VYear;
}
#Column(name = "v_category", length = 45)
public String getVCategory() {
return this.VCategory;
}
public void setVCategory(String VCategory) {
this.VCategory = VCategory;
}
#Column(name = "v_transmission", length = 45)
public String getVTransmission() {
return this.VTransmission;
}
public void setVTransmission(String VTransmission) {
this.VTransmission = VTransmission;
}
#Column(name = "v_milleage")
public Integer getVMilleage() {
return this.VMilleage;
}
public void setVMilleage(Integer VMilleage) {
this.VMilleage = VMilleage;
}
#Column(name = "v_plate", length = 45)
public String getVPlate() {
return this.VPlate;
}
public void setVPlate(String VPlate) {
this.VPlate = VPlate;
}
#Column(name = "v_color", length = 45)
public String getVColor() {
return this.VColor;
}
public void setVColor(String VColor) {
this.VColor = VColor;
}
#Column(name = "v_price", precision = 12, scale = 0)
public Float getVPrice() {
return this.VPrice;
}
public void setVPrice(Float VPrice) {
this.VPrice = VPrice;
}
#ManyToMany(fetch = FetchType.LAZY)
// #JsonBackReference
#JoinTable(name = "order_has_vehicle", catalog = "crud", joinColumns = {
#JoinColumn(name = "vehicle_v_id", nullable = false, updatable = false) }, inverseJoinColumns = {
#JoinColumn(name = "order_o_id", nullable = false, updatable = false),
#JoinColumn(name = "order_customer_cust_id", nullable = false, updatable = false),
#JoinColumn(name = "order_employee_emp_id", nullable = false, updatable = false) })
public Set<Order> getOrders() {
return this.orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
OrderServiceImpl.java
package com.jdm.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jdm.dto.OrderDTO;
import com.jdm.dto.OrderIdDTO;
import com.jdm.dto.VehicleDTO;
import com.jdm.entity.Customer;
import com.jdm.entity.Order;
import com.jdm.entity.OrderId;
import com.jdm.entity.Vehicle;
import com.jdm.repository.CustomerRepository;
import com.jdm.repository.OrderRepository;
import com.jdm.repository.VehicleRepository;
import com.jdm.services.CustomerService;
import com.jdm.services.OrderService;
#Service
public class OrderServiceImpl implements OrderService{
#Autowired
private OrderRepository orderRepo;
#Autowired
private VehicleRepository vehicleRepo;
public List<Order> findAllOrder(){
return (List<Order>) orderRepo.findAll();
}
public Optional<Order> findOrderById(Integer id) {
return orderRepo.findById(id);
}
public Order saveOrder(OrderDTO orderdto) {
Order order = new Order();
OrderId oid = new OrderId();
oid.setOId(orderdto.getId().getOId());
oid.setEmployeeEmpId(orderdto.getId().getEmployeeEmpId());
oid.setCustomerCustId(orderdto.getId().getCustomerCustId());
order.setId(oid);
order.setDate(orderdto.getDate());
order.setTotal(orderdto.getTotal());
// order.setVehicles(orderdto.getVehicles());
// Vehicle vehicle = new Vehicle();
// vehicle.getOrders().add(order);
// Set<Order> orderlist = new HashSet<Order>();
// Vehicle v = new Vehicle();
// orderlist.add(order);
// v.setOrders(orderlist);
Set<Vehicle> vehiclelist = new HashSet<Vehicle>();
vehicleRepo.findAll().forEach(vehiclelist::add);
for(Vehicle v: vehiclelist) {
Set<Vehicle> vehicle = new HashSet<Vehicle>();
Vehicle vdto = new Vehicle();
vdto.setVId(v.getVId());
vdto.setVManufacturer(v.getVManufacturer());
vdto.setVModel(v.getVModel());
vdto.setVYear(v.getVYear());
vdto.setVMilleage(v.getVMilleage());
vdto.setVPlate(v.getVPlate());
vdto.setVTransmission(v.getVTransmission());
vdto.setVColor(v.getVColor());
vdto.setVCategory(v.getVCategory());
vdto.setVPrice(v.getVPrice());
order.getVehicles().add(vdto);
// Set<Order> orderlist = new HashSet<Order>();
// orderlist.add(order);
// v.setOrders(orderlist);
}
return orderRepo.save(order);
}
public void deleteOrder(Integer id) {
orderRepo.deleteById(id);
}
public void updateOrder(Order order) {
orderRepo.save(order);
}
}
ERD Diagram
Like #XtremeBaumer said, you need to remove the updatable=false attribute for updation to work. The attribute is normally used when you want to tell a bidirectional relation that the owning side of the relation will be responsible for the updation .
For instance, in your case suppose your Employee or Customer table has an Address entity.So in the address entity class ,suppose you have a One to Many relation with the Employee or Customer entity.In this case , you could provide the updatable=false attribute on the Employee or Customer entity class in the Address entity to tell Hibernate that it is not the responsibility of the Address entity class to update the Customer or Employee entities.
Also, you need to define the owning side of the relation in your code correctly.In your code , you are providing the join condition in both the entity classes. Read.
Also, in you code the Entity class is being directly returned from the endpoint. It is advisable to return a DTO object from your service class instead of the Entity class itself, so that you could prevent the infinite reference or have better control over the data returned. If you want to know more about DTO pattern, read.
Related
I've been having issues with the Where annotation
I have two tables on MySQL 8.0.18, Venues and Concerts, with this structure and fields
CREATE TABLE VENUE
(
ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(220) NOT NULL,
IMAGE VARCHAR(240),
ACTIVE BIT NOT NULL DEFAULT 0,
COORDINATES VARCHAR(120),
COUNTRY VARCHAR(120) NOT NULL DEFAULT 'USA',
CITY VARCHAR(220) NOT NULL DEFAULT 'None',
RATING INT NOT NULL DEFAULT 0,
VERSION INT NOT NULL DEFAULT 0,
PRIMARY KEY (ID)
);
CREATE TABLE CONCERT
(
ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(120) NOT NULL,
DESCRIPTION VARCHAR(120) NOT NULL,
FESTIVAL_ID INT,
VENUE_ID INT,
RATING INT NOT NULL DEFAULT 0,
DATE DATETIME,
IMAGE BLOB,
VERSION INT NOT NULL DEFAULT 0,
FOREIGN KEY (FESTIVAL_ID) REFERENCES FESTIVAL (ID),
FOREIGN KEY (VENUE_ID) REFERENCES VENUE (ID),
PRIMARY KEY (ID)
);
The Java Hibernate and Spring Data JPA configuration is as following
package com.heatmanofurioso.gigger.service.persistencehibernateimplementation.config;
import com.heatmanofurioso.gigger.service.persistencehibernateimplementation.HibernateServiceMarker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
#Configuration
#EnableJpaRepositories(basePackageClasses = {HibernateServiceMarker.class})
#ComponentScan(basePackageClasses = {HibernateServiceMarker.class})
#Slf4j
#EnableTransactionManagement
public class HibernateUtilConfig {
private static final String MYSQL_DATA_SOURCE = "java:jboss/MysqlDataSource";
private static final String HIBERNATE_ENTITIES = "com.heatmanofurioso.gigger.service.persistencehibernateimplementation.entity";
#Bean
public DataSource dataSource() {
JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
return jndiDataSourceLookup.getDataSource(MYSQL_DATA_SOURCE);
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(HIBERNATE_ENTITIES);
log.info("Created entity manager successfully");
JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
//Properties to show SQL format of tables on deploy
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.show_sql", true); // Mark as true to log hibernate queries
jpaProperties.put("hibernate.format_sql", true); // Mark as true to log hibernate queries
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
return entityManagerFactoryBean;
}
#Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
#Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
package com.heatmanofurioso.gigger.service.persistencehibernateimplementation.dao;
import com.heatmanofurioso.gigger.service.persistencehibernateimplementation.entity.VenueEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository
public interface VenueHibernateServiceImpl extends JpaRepository<VenueEntity, Long> {
#Query("SELECT t FROM VenueEntity t WHERE t.name like ?1")
List<VenueEntity> getByName(String name);
#Query("SELECT t FROM VenueEntity t WHERE t.coordinates = ?1")
List<VenueEntity> getByCoordinates(String coordinates);
}
package com.heatmanofurioso.gigger.service.persistencehibernateimplementation.dao;
import com.heatmanofurioso.gigger.service.persistencehibernateimplementation.entity.ConcertEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository
public interface ConcertHibernateServiceImpl extends JpaRepository<ConcertEntity, Long> {
#Query("SELECT t FROM ConcertEntity t WHERE t.name like ?1")
List<ConcertEntity> getByName(String name);
}
And I am successfully obtaining the following entities using Java 11 Spring JPA 5.2.0.RELEASE and Hibernate 5.4.8 deployed on a Wildfly 18.0.0.Final using the following entities
package com.heatmanofurioso.gigger.service.persistencehibernateimplementation.entity;
import org.hibernate.annotations.Where;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import java.util.HashSet;
import java.util.Set;
#Entity
#Table(name = "VENUE")
public class VenueEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", nullable = false)
private Long id;
#Column(name = "NAME", nullable = false)
private String name;
#Column(name = "IMAGE", nullable = false)
private String image;
#Column(name = "ACTIVE", nullable = false)
private Boolean active;
#Column(name = "COORDINATES")
private String coordinates;
#Column(name = "RATING")
private Long rating;
#Column(name = "COUNTRY")
private String country;
#Column(name = "CITY")
private String city;
#OneToMany(mappedBy = "venue", fetch = FetchType.EAGER)
private Set<ConcertEntity> concerts = new HashSet<>();
#Column(name = "VERSION")
#Version
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getCoordinates() {
return coordinates;
}
public void setCoordinates(String coordinates) {
this.coordinates = coordinates;
}
public Long getRating() {
return rating;
}
public void setRating(Long rating) {
this.rating = rating;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Set<ConcertEntity> getConcerts() {
return concerts;
}
public void setConcerts(Set<ConcertEntity> concerts) {
this.concerts = concerts;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
package com.heatmanofurioso.gigger.service.persistencehibernateimplementation.entity;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;
#Entity
#Table(name = "CONCERT")
public class ConcertEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", nullable = false)
private Long id;
#Column(name = "NAME", nullable = false)
private String name;
#Column(name = "DESCRIPTION")
private String description;
#Column(name = "RATING")
private Long rating;
#Column(name = "DATE", nullable = false)
private String date;
#Column(name = "IMAGE")
private String image;
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(
name = "USER_CONCERT",
joinColumns = {#JoinColumn(name = "CONCERT_ID")},
inverseJoinColumns = {#JoinColumn(name = "USER_ID")}
)
private Set<UserEntity> userConcert = new HashSet<>();
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(
name = "CONCERT_ARTIST",
joinColumns = {#JoinColumn(name = "CONCERT_ID")},
inverseJoinColumns = {#JoinColumn(name = "ARTIST_ID")}
)
private Set<ArtistEntity> concertArtist = new HashSet<>();
#OneToOne
#JoinColumn(name = "FESTIVAL_ID", nullable = false)
private FestivalEntity festival;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "VENUE_ID")
private VenueEntity venue;
#Column(name = "VERSION")
#Version
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getRating() {
return rating;
}
public void setRating(Long rating) {
this.rating = rating;
}
public LocalDateTime getDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(date, formatter);
}
public void setDate(LocalDateTime date) {
this.date = date.toString();
}
public String getDateString() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Set<UserEntity> getUserConcert() {
return userConcert;
}
public void setUserConcert(Set<UserEntity> userConcert) {
this.userConcert = userConcert;
}
public Set<ArtistEntity> getConcertArtist() {
return concertArtist;
}
public void setConcertArtist(Set<ArtistEntity> concertArtist) {
this.concertArtist = concertArtist;
}
public FestivalEntity getFestival() {
return festival;
}
public void setFestival(FestivalEntity festival) {
this.festival = festival;
}
public VenueEntity getVenue() {
return venue;
}
public void setVenue(VenueEntity venue) {
this.venue = venue;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
The returned dataset corresponds to the following
Now, my issue is that if I try to add the #Where annotation to my Concerts collection on my VenueEntity, like so
#OneToMany(mappedBy = "venue", fetch = FetchType.EAGER)
private Set<ConcertEntity> concerts = new HashSet<>();
#OneToMany(mappedBy = "venue", fetch = FetchType.EAGER)
#Where(clause = "date < current_date")
private Set<ConcertEntity> pastConcerts = new HashSet<>();
#OneToMany(mappedBy = "venue", fetch = FetchType.EAGER)
#Where(clause = "date => current_date")
private Set<ConcertEntity> futureConcerts = new HashSet<>();
those two collections return empty datasets, even though, at the creation of the rows on the database, I am setting the value of the DATE field as current_date, so the pastConcerts collection should show results at least
Borh Sets are empty probably because current_date is null. Both predicates resolve to false in that case. It happens because hibernate cannot resolve a column named current_date and sets it to null.
You might work it around by using subselect, because clause is simply an sql predicate that gets pasted inside where on query creation (it depends on the database you're using):
#Where(clause = "date < (select * from current_date())")
Or shorter in some databases:
#Where(clause = "date < (select current_date())")
P.S.: In my opinion, this is not a good design. It's harder to test and makes every query concerning those sets unpredictible (same query can result in different results depending on time run).
I have the same association (literally the same FK column) mapped multiple times - but because Hibernate knows this is the same FK it considers them as the same.
Found out while debugging the SQL that Hibernate generated.
Essentially, this is not possible on the entity
I have 2 tables that are bind in a "One to many" connection when I set the relation to EAGER , then the records are fetched from table A and then for each record the data is fetched from table B if let’s say I have 100 record in table B then 100 select query are done which is really bad for the performance. what do I need to do to the set it correct so all the data will be fetched in 1 query?
here is all the code: for table A (survey)
package hibernateDataFiles;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
/**
* Surveys generated by hbm2java
*/
#Entity
#Table(name = "surveys", schema = "edi_ms")
public class Survey implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long surveyId;
private String umn;
private String firstName;
private String middleName;
private String lastName;
private String phoneNumber;
private Date creationDate;
private Long shortFeedbackGrade;
private String shortFeedbackComment;
private List<MemberAnswer> memberAnswers = new ArrayList<MemberAnswer>(0);
private List<CategoryAnswer> categoriesAnswers = new ArrayList<CategoryAnswer>(0);
public Survey() {
}
public Survey(long surveyId, String firstName, String lastName, String phoneNumber, Date creationDate) {
this.surveyId = surveyId;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.creationDate = creationDate;
}
public Survey(long surveyId, String umn, String firstName, String middleName, String lastName, String phoneNumber,
Date creationDate, Long shortFeedbackGrade, String shortFeedbackComment,
List<MemberAnswer> memberAnswers, List<CategoryAnswer> categoriesAnswer) {
this.surveyId = surveyId;
this.umn = umn;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.creationDate = creationDate;
this.shortFeedbackGrade = shortFeedbackGrade;
this.shortFeedbackComment = shortFeedbackComment;
this.memberAnswers = memberAnswers;
this.categoriesAnswers = categoriesAnswer;
}
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "survey_id_seq")
#SequenceGenerator(allocationSize = 1, name = "survey_id_seq", sequenceName = "EDI_MS.survey_id_seq")
#Id
#Column(name = "survey_id", unique = true, nullable = false)
public long getSurveyId() {
return this.surveyId;
}
public void setSurveyId(long surveyId) {
this.surveyId = surveyId;
}
#Column(name = "umn", length = 15)
public String getUmn() {
return this.umn;
}
public void setUmn(String umn) {
this.umn = umn;
}
#Column(name = "first_name", nullable = false, length = 20)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "middle_name", length = 20)
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
#Column(name = "last_name", nullable = false, length = 20)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name = "phone_number", nullable = false, length = 15)
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "creation_date", nullable = false, length = 29)
public Date getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
#Column(name = "short_feedback_grade")
public Long getShortFeedbackGrade() {
return this.shortFeedbackGrade;
}
public void setShortFeedbackGrade(Long shortFeedbackGrade) {
this.shortFeedbackGrade = shortFeedbackGrade;
}
#Column(name = "short_feedback_comment", length = 500)
public String getShortFeedbackComment() {
return this.shortFeedbackComment;
}
public void setShortFeedbackComment(String shortFeedbackComment) {
this.shortFeedbackComment = shortFeedbackComment;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
#NotFound(action = NotFoundAction.IGNORE)
public List<MemberAnswer> getMemberAnswers() {
return this.memberAnswers;
}
public void setMemberAnswers(List<MemberAnswer> membersAnswers) {
this.memberAnswers = membersAnswers;
}
#OneToMany(fetch = FetchType.EAGER, mappedBy = "survey")
#NotFound(action = NotFoundAction.IGNORE)
public List<CategoryAnswer> getCategoriesAnswers() {
return this.categoriesAnswers;
}
public void setCategoriesAnswers(List<CategoryAnswer> categoriesAnswers) {
this.categoriesAnswers = categoriesAnswers;
}
}
here is the JPA
import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import hibernateDataFiles.Survey;
public interface SurveyRepository extends JpaRepository<Survey, Long> {
#Query("from Survey where ?1 <= creation_date and creation_date < ?2 ")
List<Survey> getSurveysByDates(Date fromDate , Date toDate );
}
here is table B (category_answers)
package hibernateDataFiles;
// Generated Jul 5, 2018 8:37:29 AM by Hibernate Tools 5.2.10.Final
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* CategoriesAnswers generated by hbm2java
*/
#Entity
#Table(name = "categories_answers", schema = "edi_ms")
public class CategoryAnswer implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private CategoryAnswerId id;
private Category category;
private Survey survey;
private long grade;
private String comment;
public CategoryAnswer() {
}
public CategoryAnswer(CategoryAnswerId id, Category category, Survey survey, long grade) {
this.id = id;
this.category = category;
this.survey = survey;
this.grade = grade;
}
public CategoryAnswer(CategoryAnswerId id, Category category, Survey survey, long grade, String comment) {
this.id = id;
this.category = category;
this.survey = survey;
this.grade = grade;
this.comment = comment;
}
#EmbeddedId
#AttributeOverrides({ #AttributeOverride(name = "surveyId", column = #Column(name = "survey_id", nullable = false)),
#AttributeOverride(name = "categoryId", column = #Column(name = "category_id", nullable = false)) })
public CategoryAnswerId getId() {
return this.id;
}
public void setId(CategoryAnswerId id) {
this.id = id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "category_id", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "survey_id", nullable = false, insertable = false, updatable = false)
#JsonIgnore
public Survey getSurvey() {
return this.survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
#Column(name = "grade", nullable = false)
public long getGrade() {
return this.grade;
}
public void setGrade(long grade) {
this.grade = grade;
}
#Column(name = "comment", length = 500)
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
and categoryAnswerId (pk of the table )
package hibernateDataFiles;
// Generated Jul 5, 2018 8:37:29 AM by Hibernate Tools 5.2.10.Final
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
/**
* CategoriesAnswersId generated by hbm2java
*/
#Embeddable
public class CategoryAnswerId implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long surveyId;
private long categoryId;
public CategoryAnswerId() {
}
public CategoryAnswerId(long surveyId, long categoryId) {
this.surveyId = surveyId;
this.categoryId = categoryId;
}
#Column(name = "survey_id", nullable = false)
public long getSurveyId() {
return this.surveyId;
}
public void setSurveyId(long surveyId) {
this.surveyId = surveyId;
}
#Column(name = "category_id", nullable = false)
public long getCategoryId() {
return this.categoryId;
}
public void setCategoryId(long categoryId) {
this.categoryId = categoryId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof CategoryAnswerId))
return false;
CategoryAnswerId castOther = (CategoryAnswerId) other;
return (this.getSurveyId() == castOther.getSurveyId()) && (this.getCategoryId() == castOther.getCategoryId());
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getSurveyId();
result = 37 * result + (int) this.getCategoryId();
return result;
}
}
and the JPA:
package repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import hibernateDataFiles.Category;
public interface CategoryRepository extends JpaRepository<Category, Long>{
#Transactional
#Modifying
#Query("update Category set expiration_date = current_date() where category_id = ?1 ")
void expireCategory(Long id );
#Query("from Category where function ('coalesce' ,effectiveDate ,current_date() ) <= current_date() "
+ "and function('coalesce' ,expirationDate , to_date('50001231','yyyymmdd')) > current_date() ")
List<Category> getEffective( );
}
You have the so called 1+n problem.
One solution is to tweak the fetch settings of the collection.
Can't find a JPA way on the short hand. But since your using Hibernate, this should work:
#org.hibernate.annotations.Fetch(FetchMode.JOIN) or better FetchMode.SUBSELECT
If you're executing a query, you have to adjust the query by adding join fetch.
I have read so much topics and still I cant solve my problem.
Problem: error from title. I get it every time I try to read entity from database.
Database is mysql one, connected by jdbc driver.
Here is my code: First one is ReadStudentDemo (to read from database)
package entities;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityTransaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class ReadStudentDemo {
public static void main(String[] args) throws ParseException {
Player roggy = new Player();
Configuration conf = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Player.class);
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);
Session session = sf.openSession();
EntityTransaction tx = session.beginTransaction();
roggy = (Player)session.get(Player.class, 4);
tx.commit();
System.out.println(roggy);
}
}
second is Player.java with Player entity mapped.
package entities;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Player generated by hbm2java
*/
#Entity
#Table(name = "Player", catalog = "sql11217012")
public class Player implements java.io.Serializable {
private Integer id;
private String firstName;
private String lastName;
private Date birth;
private Float wl;
private Integer win;
private BigDecimal money;
private Set<Match> matchesForPlayerId = new HashSet<Match>(0);
private Set<Match> matchesForPlayer1Id = new HashSet<Match>(0);
private Set<Match> matchesForCourtId = new HashSet<Match>(0);
public Player() {
}
public Player(String firstName, String lastName, Date birth, Float wl, Integer win, BigDecimal money,
Set<Match> matchesForPlayerId, Set<Match> matchesForPlayer1Id, Set<Match> matchesForCourtId) {
this.firstName = firstName;
this.lastName = lastName;
this.birth = birth;
this.wl = wl;
this.win = win;
this.money = money;
this.matchesForPlayerId = matchesForPlayerId;
this.matchesForPlayer1Id = matchesForPlayer1Id;
this.matchesForCourtId = matchesForCourtId;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name = "first_name", length = 45)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "last_name", length = 45)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Temporal(TemporalType.DATE)
#Column(name = "birth", length = 10)
public Date getBirth() {
return this.birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
#Column(name = "wl", precision = 12, scale = 0)
public Float getWl() {
return this.wl;
}
public void setWl(Float wl) {
this.wl = wl;
}
#Column(name = "win")
public Integer getWin() {
return this.win;
}
public void setWin(Integer win) {
this.win = win;
}
#Column(name = "money", precision = 15)
public BigDecimal getMoney() {
return this.money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByPlayerId")
public Set<Match> getMatchesForPlayerId() {
return this.matchesForPlayerId;
}
public void setMatchesForPlayerId(Set<Match> matchesForPlayerId) {
this.matchesForPlayerId = matchesForPlayerId;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByPlayer1Id")
public Set<Match> getMatchesForPlayer1Id() {
return this.matchesForPlayer1Id;
}
public void setMatchesForPlayer1Id(Set<Match> matchesForPlayer1Id) {
this.matchesForPlayer1Id = matchesForPlayer1Id;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByCourtId")
public Set<Match> getMatchesForCourtId() {
return this.matchesForCourtId;
}
public void setMatchesForCourtId(Set<Match> matchesForCourtId) {
this.matchesForCourtId = matchesForCourtId;
}
}
Then you have Match entity.
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
#Entity
#Table(name = "Match", catalog = "sql11217012")
public class Match implements java.io.Serializable {
private Integer id;
private Player playerByPlayerId;
private Player playerByPlayer1Id;
private Player playerByCourtId;
public Match() {
}
public Match(Player playerByPlayerId, Player playerByPlayer1Id, Player playerByCourtId) {
this.playerByPlayerId = playerByPlayerId;
this.playerByPlayer1Id = playerByPlayer1Id;
this.playerByCourtId = playerByCourtId;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "player_id", nullable = false)
public Player getPlayerByPlayerId() {
return this.playerByPlayerId;
}
public void setPlayerByPlayerId(Player playerByPlayerId) {
this.playerByPlayerId = playerByPlayerId;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "player1_id", nullable = false)
public Player getPlayerByPlayer1Id() {
return this.playerByPlayer1Id;
}
public void setPlayerByPlayer1Id(Player playerByPlayer1Id) {
this.playerByPlayer1Id = playerByPlayer1Id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "court_id", nullable = false)
public Player getPlayerByCourtId() {
return this.playerByCourtId;
}
public void setPlayerByCourtId(Player playerByCourtId) {
this.playerByCourtId = playerByCourtId;
}
}
And then the stack trace
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" org.hibernate.AnnotationException: Use of #OneToMany or #ManyToMany targeting an unmapped class: entities.Player.matchesForCourtId[entities.Match]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1253)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:810)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:735)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at entities.ReadStudentDemo.main(ReadStudentDemo.java:50)
Thanks in advance for any help!
Well you add Player to your Configuration
Configuration conf = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Player.class);
but seems you never add Match.class?
So probably you should use
Configuration conf = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Player.class)
.addAnnotatedClass(Match.class);
I'm facing a little issue : I want to select the campaigns where the average of campaignProgression equals 100 using criteria :
here is the query in SQL :
SELECT * FROM campagne as camp, progression_campagne as campProg
where camp.id = campProg.current_campaign and
(SELECT avg(campaign_progression) FROM progression_campagne)=100 group by camp.id;
here is the translation in criteria :
Criteria crit = getSessionFactory().getCurrentSession().createCriteria(Campagne.class);
crit.createAlias("progressionCampagnes", "prog");
crit.setProjection(Projections.projectionList().add(Projections.groupProperty("prog.campagne")).add(Projections.avg("prog.campaignProgression"),"moy"));
crit.add(Restrictions.eq("moy", new Float(100)));
But I'm getting this error :
org.hibernate.QueryException: could not resolve property: moy of:
ma.dataprotect.sensipro.model.Campagne
Edited :
Here are the model classes of Campaign and ProgressionCampaign :
Campagne.java :
package ma.dataprotect.sensipro.model;
// Generated 10 ao�t 2015 14:36:11 by Hibernate Tools 3.4.0.CR1
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import org.hibernate.Hibernate;
import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Campagne generated by hbm2java
*/
#SuppressWarnings("serial")
#Entity
#Table(name = "campagne")
public class Campagne implements java.io.Serializable {
private Long id;
private String name;
private Organism organism;
private byte[] image;
private Date launchDate;
private Date endDate;
private String description;
private Set<User> users = new HashSet<User>();
private Set<ProgressionCampagne> progressionCampagnes = new HashSet<ProgressionCampagne>();
private Set<ProgressionCours> progressionCourses = new HashSet<ProgressionCours>();
private Long notificationid;
private Boolean isStdr;
private Set<Cours> courses = new HashSet<Cours>();
public Campagne() {
}
public Campagne(String name, Date launchDate) {
this.name = name;
this.launchDate = launchDate;
}
public Campagne(String name, String description) {
this.name = name;
this.description = description;
}
public Campagne(String name, String description, byte[] image, Organism org) {
this.name = name;
this.description = description;
this.image = image;
this.organism = org;
}
public Campagne(String name, Date launchDate, Date endDate,
String description, Set<User> users,
Set<ProgressionCampagne> progressionCampagnes,
Set<ProgressionCours> progressionCourses) {
this.name = name;
this.launchDate = launchDate;
this.endDate = endDate;
this.description = description;
this.users = users;
this.progressionCampagnes = progressionCampagnes;
this.progressionCourses = progressionCourses;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "name", nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Temporal(TemporalType.DATE)
#Column(name = "launchDate", length = 10)
public Date getLaunchDate() {
return this.launchDate;
}
public void setLaunchDate(Date launchDate) {
this.launchDate = launchDate;
}
#Temporal(TemporalType.DATE)
#Column(name = "endDate", length = 10)
public Date getEndDate() {
return this.endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
#Column(name = "description")
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
#Column(name = "notificationid")
public Long getNotificationid() {
return notificationid;
}
public void setNotificationid(Long notificationid) {
this.notificationid = notificationid;
}
#ManyToMany
#JoinTable(name = "user_campagne", joinColumns = { #JoinColumn(name = "campagneid", nullable = false, updatable = true) }, inverseJoinColumns = { #JoinColumn(name = "userid", nullable = false, updatable = true) })
public Set<User> getUsers() {
// Hibernate.initialize(users);
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
#OneToMany(mappedBy = "campagne")
public Set<ProgressionCampagne> getProgressionCampagnes() {
return this.progressionCampagnes;
}
public void setProgressionCampagnes(
Set<ProgressionCampagne> progressionCampagnes) {
this.progressionCampagnes = progressionCampagnes;
}
#OneToMany(mappedBy = "campagne")
public Set<ProgressionCours> getProgressionCourses() {
return this.progressionCourses;
}
public void setProgressionCourses(Set<ProgressionCours> progressionCourses) {
this.progressionCourses = progressionCourses;
}
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "organismsid", nullable = false)
public Organism getOrganism() {
return this.organism;
}
public void setOrganism(Organism organism) {
this.organism = organism;
}
#Column(name = "image", length = 5242880)
public byte[] getImage() {
return this.image;
}
public void setImage(byte[] image) {
this.image = image;
}
#Column(name = "isStdr", nullable = false, length = 0, columnDefinition = "bit")
public Boolean getIsStdr() {
return isStdr;
}
public void setIsStdr(Boolean isStdr) {
this.isStdr = isStdr;
}
#ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
#JoinTable(name = "campagne_cours", joinColumns = { #JoinColumn(name = "campagne_id") }, inverseJoinColumns = { #JoinColumn(name = "cours_id") })
public Set<Cours> getCourses() {
return courses;
}
public void setCourses(Set<Cours> courses) {
this.courses = courses;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Campagne other = (Campagne) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
ProgressionCampagne.java
package ma.dataprotect.sensipro.model;
// Generated 10 ao�t 2015 14:36:11 by Hibernate Tools 3.4.0.CR1
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* ProgressionCampagne generated by hbm2java
*/
#SuppressWarnings("serial")
#Entity
#Table(name = "progression_campagne")
public class ProgressionCampagne implements java.io.Serializable {
private Long id;
private Campagne campagne;
private User user;
private Cours cours;
private float campaignProgression;
private float campaignScore;
private Float campagneCorrectAnswer;
private Long campagneNbrEssai;
private Date dateDebutCamp;
private Date dateFinCamp;
public ProgressionCampagne() {
}
public ProgressionCampagne(Campagne campagne, User user, Cours cours,
float campaignProgression, float campaignScore,
Date dateDebutCamp, Date dateFinCamp) {
super();
this.campagne = campagne;
this.user = user;
this.cours = cours;
this.campaignProgression = campaignProgression;
this.campaignScore = campaignScore;
this.dateDebutCamp = dateDebutCamp;
this.dateFinCamp = dateFinCamp;
}
public ProgressionCampagne(Campagne campagne, User user, Cours cours,
int campaignProgression) {
this.campagne = campagne;
this.user = user;
this.cours = cours;
this.campaignProgression = campaignProgression;
}
public ProgressionCampagne(Campagne campagne, User user, Cours cours,
int campaignProgression,Date dateDebut) {
this.campagne = campagne;
this.user = user;
this.cours = cours;
this.campaignProgression = campaignProgression;
this.dateDebutCamp=dateDebut;
}
public ProgressionCampagne(Campagne campagne, User user,
int campaignProgression,Date dateDebut) {
this.campagne = campagne;
this.user = user;
this.campaignProgression = campaignProgression;
this.dateDebutCamp=dateDebut;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
#ManyToOne
#JoinColumn(name = "current_campaign", nullable = false)
public Campagne getCampagne() {
return this.campagne;
}
public void setCampagne(Campagne campagne) {
this.campagne = campagne;
}
#ManyToOne
#JoinColumn(name = "usersid", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
#ManyToOne
#JoinColumn(name = "current_course", nullable = true)
// false
public Cours getCours() {
return this.cours;
}
public void setCours(Cours cours) {
this.cours = cours;
}
#Column(name = "campaign_progression", nullable = false)
public float getCampaignProgression() {
return this.campaignProgression;
}
public void setCampaignProgression(float campaignProgression) {
this.campaignProgression = campaignProgression;
}
#Column(name = "campaign_score", nullable = true)
public float getCampaignScore() {
return campaignScore;
}
public void setCampaignScore(float campaignScore) {
this.campaignScore = campaignScore;
}
#Column(name = "date_debut_camp", nullable = true)
public Date getDateDebutCamp() {
return dateDebutCamp;
}
public void setDateDebutCamp(Date dateDebutCamp) {
this.dateDebutCamp = dateDebutCamp;
}
#Column(name = "date_fin_camp", nullable = true)
public Date getDateFinCamp() {
return dateFinCamp;
}
public void setDateFinCamp(Date dateFinCamp) {
this.dateFinCamp = dateFinCamp;
}
#Column(name = "campagne_correct_answer", nullable = true)
public Float getCampagneCorrectAnswer() {
return campagneCorrectAnswer;
}
public void setCampagneCorrectAnswer(Float campagneCorrectAnswer) {
this.campagneCorrectAnswer = campagneCorrectAnswer;
}
#Column(name = "campagne_nbr_essai", nullable = true)
public Long getCampagneNbrEssai() {
return campagneNbrEssai;
}
public void setCampagneNbrEssai(Long campagneNbrEssai) {
this.campagneNbrEssai = campagneNbrEssai;
}
}
Ok, in your criteria you have to use a subquery in order to achieve the HAVING feature. This is the code:
DetachedCriteria innerCrit = DetachedCriteria.forClass(Campagne.class);
innerCrit.createAlias("progressionCampagnes", "prog");
innerCrit.setProjection(Projections.avg("prog.campaignProgression");
innerCrit.add(Restrictions.eqProperty("id", "campOuter.id"));
DetachedCriteriaouterCrit = DetachedCriteria.forClass(Campagne.class, "campOuter");
outerCrit.add(Subqueries.eq(100, innerCrit));
This should get you the original sql result.
I am generating my Annotated Code Classes with hibernate and eclipse JBoss plugin, For the One to Many & Many to Many Relationships etc, the code generated used Set type for collections and generate many to many as one to many relation.
Project:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Project generated by hbm2java
*/
#Entity
#Table(name = "project", schema = "public")
public class Project implements java.io.Serializable
{
private int projectId;
private Type type;
private Customer customer;
private String projectName;
private String description;
private Double totalContractAmount;
private Set projectConstructionMaterials = new HashSet( 0 );
public Project()
{
}
public Project(int projectId)
{
this.projectId = projectId;
}
public Project(int projectId, Type type, Customer customer, String projectName, String description,
Double totalContractAmount,
Set projectConstructionMaterials)
{
this.projectId = projectId;
this.type = type;
this.customer = customer;
this.projectName = projectName;
this.description = description;
this.totalContractAmount = totalContractAmount;
this.projectConstructionMaterials = projectConstructionMaterials;
}
#Id
#Column(name = "project_id", unique = true, nullable = false)
public int getProjectId()
{
return this.projectId;
}
public void setProjectId(int projectId)
{
this.projectId = projectId;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "type_id")
public Type getType()
{
return this.type;
}
public void setType(Type type)
{
this.type = type;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "customer_id")
public Customer getCustomer()
{
return this.customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
#Column(name = "project_name")
public String getProjectName()
{
return this.projectName;
}
public void setProjectName(String projectName)
{
this.projectName = projectName;
}
#Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "project") //it should be many to many annotation
public Set getProjectConstructionMaterials()
{
return this.projectConstructionMaterials;
}
public void setProjectConstructionMaterials(Set projectConstructionMaterials)
{
this.projectConstructionMaterials = projectConstructionMaterials;
}
}
ProjectConstructionMaterial
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* ProjectConstructionMaterial generated by hbm2java
*/
#Entity
#Table(name = "project_construction_material", schema = "public")
public class ProjectConstructionMaterial implements java.io.Serializable
{
private int projectConstructionMaterialId;
private ConstructionMaterialInventory constructionMaterialInventory;
private Project project;
private String description;
private String quantityArrived;
private String quantityConsumed;
public ProjectConstructionMaterial()
{
}
public ProjectConstructionMaterial(int projectConstructionMaterialId)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
}
public ProjectConstructionMaterial(int projectConstructionMaterialId,
ConstructionMaterialInventory constructionMaterialInventory, Project project, String description,
String quantityArrived, String quantityConsumed)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
this.constructionMaterialInventory = constructionMaterialInventory;
this.project = project;
this.description = description;
this.quantityArrived = quantityArrived;
this.quantityConsumed = quantityConsumed;
}
#Id
#Column(name = "project_construction_material_id", unique = true, nullable = false)
public int getProjectConstructionMaterialId()
{
return this.projectConstructionMaterialId;
}
public void setProjectConstructionMaterialId(int projectConstructionMaterialId)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "construction_material_id")
public ConstructionMaterialInventory getConstructionMaterialInventory()
{
return this.constructionMaterialInventory;
}
public void setConstructionMaterialInventory(ConstructionMaterialInventory constructionMaterialInventory)
{
this.constructionMaterialInventory = constructionMaterialInventory;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "project_id")
public Project getProject()
{
return this.project;
}
public void setProject(Project project)
{
this.project = project;
}
#Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
#Column(name = "quantity_arrived")
public String getQuantityArrived()
{
return this.quantityArrived;
}
public void setQuantityArrived(String quantityArrived)
{
this.quantityArrived = quantityArrived;
}
#Column(name = "quantity_consumed")
public String getQuantityConsumed()
{
return this.quantityConsumed;
}
public void setQuantityConsumed(String quantityConsumed)
{
this.quantityConsumed = quantityConsumed;
}
}
ConstructionMaterialInventory
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* ConstructionMaterialInventory generated by hbm2java
*/
#Entity
#Table(name = "construction_material_inventory", schema = "public")
public class ConstructionMaterialInventory implements java.io.Serializable
{
private int constructionMaterialId;
private Type type;
private SubType subType;
private String quantity;
private String description;
private Set projectConstructionMaterials = new HashSet( 0 );
public ConstructionMaterialInventory()
{
}
public ConstructionMaterialInventory(int constructionMaterialId)
{
this.constructionMaterialId = constructionMaterialId;
}
public ConstructionMaterialInventory(int constructionMaterialId, Type type, SubType subType, String quantity,
String description, Set projectConstructionMaterials)
{
this.constructionMaterialId = constructionMaterialId;
this.type = type;
this.subType = subType;
this.quantity = quantity;
this.description = description;
this.projectConstructionMaterials = projectConstructionMaterials;
}
#Id
#Column(name = "construction_material_id", unique = true, nullable = false)
public int getConstructionMaterialId()
{
return this.constructionMaterialId;
}
public void setConstructionMaterialId(int constructionMaterialId)
{
this.constructionMaterialId = constructionMaterialId;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "type_id")
public Type getType()
{
return this.type;
}
public void setType(Type type)
{
this.type = type;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "sub_type_id")
public SubType getSubType()
{
return this.subType;
}
public void setSubType(SubType subType)
{
this.subType = subType;
}
#Column(name = "quantity")
public String getQuantity()
{
return this.quantity;
}
public void setQuantity(String quantity)
{
this.quantity = quantity;
}
#Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "constructionMaterialInventory")
public Set getProjectConstructionMaterials()
{
return this.projectConstructionMaterials;
}
public void setProjectConstructionMaterials(Set projectConstructionMaterials)
{
this.projectConstructionMaterials = projectConstructionMaterials;
}
}
Data Model
I want to change it to many to many annotations and also want to use List or ArrayList instead of Set. Can anyone please tell me how to do this with automatically code generation? Beause i have dozens of table like this and change in tables manually takes too much time..
For Example: Annotation Must be generate automatically as described in this example.
Many to Many annotation Example