public class OrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="order_id")
private int orderid;
#ManyToOne
#JoinColumn(name="user_id", nullable=false)
private UserEntity user;
#OneToMany(mappedBy="ordersBid",fetch=FetchType.EAGER)
private Set<BidPriceEntity> bidOrders;
}
public class BidPriceEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#ManyToOne
#JoinColumn(name="order_Id",nullable=false)
private OrderEntity ordersBid;
#ManyToOne
#JoinColumn(name="driver_Id",nullable=false)
private UserEntity driver;
#Column(name="bid_price")
private double bidPrice;
}
public class OrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="order_id")
private int orderid;
#ManyToOne
#JoinColumn(name="user_id", nullable=false)
private UserEntity user;
#OneToMany(mappedBy="ordersBid",fetch=FetchType.EAGER)
private Set<BidPriceEntity> bidOrders;
}
Here are the three entities.I am trying to tansfer the data(retrieved from database) to JsonArray.it always get error :There is a cycle in the hierarchy!
how should I set the : JsonConfig (setJsonPropertyFilter) properties to get
rid of this error.
Basically there are two options for cycle - write own serializer, use #JsonIgnore. Like in your code - OrderEntity contains list of BidPriceEntities, which in turn contains reference to OrderEntity. Mark ordersBid with #JsonIgnore and it should work. At list serialization... If you don't have access to the class - try mixins.
Related
I am getting this error because of #OneToOne. Cannot call sendError() after the response has been committed Can someone help me figure how to resolve it?
Here are my models :
#Entity
#SequenceGenerator(name = "RECOMMENDATION_SQ", sequenceName = "recommendation_sequence")
public class Review {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "RECOMMENDATION_SQ")
private Long id;
#ManyToOne
private Restaurant restaurant;
#ManyToOne
private User user;
private Date date;
#Lob
private byte[] image;
private String text;
#OneToOne(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
#JoinColumn(name="id_rating")
private Rating rating;
--
#Entity
#SequenceGenerator(name = "RATING_SQ", sequenceName = "rating_sequence")
public class Rating {
#Id
#GeneratedValue
private Long id_rating;
#OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "rating")
#JsonIgnore
private Review review;
private int dish;
private int service;
private int price;
private int location;
private int accessibility;
I tried adding #JsonIgnore (this solution : Spring Boot : Error :Cannot call sendError() after the response has been committed) but I get this error :
InvalidDefinitionException: No serializer found for class
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create
BeanSerializer
I also tried removing the fetch type and it didn't work either.
So the solution that worked for me is I used the #MapsId in one of the models and removed the field from the other class.
With #MapsId you don't need a bidirectional association.
For more details you can read this article : https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/
#Entity
#SequenceGenerator(name = "RECOMMENDATION_SQ", sequenceName =
"recommendation_sequence")
public class Review {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "RECOMMENDATION_SQ")
private Long id;
#ManyToOne
private Restaurant restaurant;
#ManyToOne
private User user;
private Date date;
#Lob
private byte[] image;
private String text;
#OneToOne(fetch = FetchType.LAZY)
#MapsId
#JoinColumn(name = "id")
private Rating rating;
--
#Entity
#SequenceGenerator(name = "RATING_SQ", sequenceName = "rating_sequence")
public class Rating {
#Id
#GeneratedValue
private Long id;
private int dish;
private int service;
private int price;
private int location;
private int accessibility;
So here is an example close to my actual code:
#Entity
#Table(name="Products")
#Data
public class Product{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="productno")
private long ID;
#Column(name="name")
private String productName;
private ProductCategory category;
}
#Entity
#Table(name="Category")
#Data
public class ProductCategory{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="categoryno")
private long ID;
#Column(name="name")
private String categoryName;
#OneToMany
#JoinColumn(name="category")
private List<Product> product;
}
aswell as a thymeleaf template, that displays a bunch of those in a table using th:each
and a controller that sends the data to those templates. Those templates have been tested and they should work.
the problem is, when bootRunning and opening the page, i get a error:
SQL.SyntaxErrorException: Unknown column 'product0_.productCategory_category'
Looking at this error, it seems it tried to read the join column name as productCategory_category instead of just category (which is the desired one)
#JoinColumn needs to go on Product class on ProductCategory field and name="categoryno"
So i've solved this thing with
#Entity
#Table(name="Products")
#Data
public class Product{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="productno")
private long ID;
#Column(name="name")
private String productName;
#ManyToOne
#JoinColumn(name="category", referencedColumnName="categoryno")
private ProductCategory category;
}
#Entity
#Table(name="Category")
#Data
public class ProductCategory{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="categoryno")
private long ID;
#Column(name="name")
private String categoryName;
#OneToMany(mappedBy="category")
private List<Product> product;
}
i have no idea why this works, but hey: magic
I have two classes and I want to have a one to many relation between them, for e.g.:
Home(id<int>, rooms<string>)
Vehicle(id<int>, home_id<int>, name<string>)
I need to have a relation between Home and Vehicle class using Home.id and vehicle.home_id.
Please suggest any example which I can use here for CURD operation to implement REST service.
I need to have a relation between Home and Vehicle class using Home.id
and vehicle.home_id.
Your entities should look like this :
Vehicle Entity
#Entity
#Table(name = "vehicle", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "name")
private String name;
#JoinColumn(name = "home_id", referencedColumnName = "id")
#ManyToOne
private Home homeId;
//constructor getter & setters
}
Home Entity
#Entity
#Table(name = "home", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Home implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "room")
private Character room;
#OneToMany(mappedBy = "homeId")
private List<Vehicle> vehicleList;
//constructor getter & setters
}
We have the following domain:
Members and Channels.
Member - equivalent of a user.
#Entity
#Table(name = "member")
public class Member {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "first_name")
private String firstName;
}
Channel - Will group blog posts by topic.
#Entity
#Table(name = "channel")
public class Channel {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
}
The members can be assigned to channels thus becoming members in the channel. So we have the ChannelMember entity since we don't always want to retrieve both ends of the relationship.
#Entity
#Table(name = "channel_member")
public class ChannelMember {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToOne
#JoinColumn(name = "channel_id", nullable = false)
private Channel channel;
#OneToOne
#JoinColumn(name = "member_id", nullable = false)
private Member member;
}
We use Spring Data JPA and MySQL for our persistance(Spring Boot 1.5.3 Release).
Why is the distinct mandatory in the JPQL query for Hibernate to be able to fetch any records in the following case?
Repository code example as follows:
#Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
#Query("select cm.member from ChannelMember cm")
List<Member> getNotWorking(); // empty List will be returned
#Query("select distinct cm.member from ChannelMember cm")
List<Member> getWorking();
}
I am new to Hibernate and I am using it with Spring. I have the following tables:
#Entity
#Configurable
public class Location {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long locationid;
private String locationName;
#ManyToOne
private Site site;
//getters setters skipped
}
#Entity
#Configurable
public class Site {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long siteid;
private String siteName;
#ManyToOne
private Country country;
//getters setters skipped
}
#Entity
#Configurable
public class Country {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long countryid;
private String countryName;
#ManyToOne
private Region region;
//getters setters skipped
}
#Entity
#Configurable
public class Region {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long regionid;
private String regionName;
//getters setters skipped
}
public class Assets {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long assetId;
#ManyToOne
private Location location;
//getters setters skipped
}
I want to pull all assets based on Regions. How should I do that?
How do I use the relationship between Region --> Country --> Site --> Location and pull the relevant records? How do I do it without affecting the performance?
Or should I just redesign the tables?
You can just complete it by a #NamedQuery in the class Assets :
select a from Asserts a where a.location.site.contry.regin.reginName = :reginName
Hope help.