JPA serialize enttiy - json

I have my data model that contains 3 tables: User, Profile, UserProfile.
public class User implements Serializable {
private Integer id;
......
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch =
FetchType.LAZY)
#JsonManagedReference
#JsonProperty("profiles")
private List<UserProfile> userProfiles = new ArrayList<UserProfile>();
}
public class Profile implements Serializable {
private Integer id;
......
#OneToMany(mappedBy="profile", cascade = CascadeType.ALL, fetch =
FetchType.LAZY)
#JsonBackReference
private List<UserProfile> userProfiles= new ArrayList<UserProfile>();
}
public class UserProfile implements Serializable {
private Integer id;
#ManyToOne
#JoinColumn(name = "idUser")
#JsonBackReference
private User user;
#ManyToOne
#JoinColumn(name = "idProfile")
#JsonManagedReference
private Profile profile;
}
And here’s my json feed back:
{
"id": 1,
.......
"profiles": [
{
"profile": {
"id": 1,
.....
},
{
"id": 2,
.....
}
}
]
}
I have two questions:
Is it possible to remove the profile attribute and have:
{
"id": 1,
.......
"profiles": [
{
"id": 1,
.....
},
{
": 2,
.....
}
]
}
In a manytomany relationship with an intermediate table that contains a primary key (id), 2 foreign key that are the ids of the 2 tables that have the manytomany relationship, is that how to do it?

For the 1st question, to hide profile attribute, there are 2 options:
1. If you don't need it in any json output, you can add a #JsonIgnore annotation to it;
2. If you need it elsewhere but don't want it here, you can use Projection. Check https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections and https://www.baeldung.com/spring-data-rest-projections-excerpts for reference on how to use projections.

Checked your code again. Your code has some problem.
You only need 2 entities: User and Profile. And just add #ManyToMany relationship to them.
Refer here for a complete sample on ManyToMany https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

Related

How to implement a JPA birectional relation for entities that works from both sides

Another question regarding bi-directional relationship with JPA entities:
Suppose I have the following two classes:
#Entity
public class Product
{
#Id
private Long productID;
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JsonManagedReference
#JoinColumn(name = "fk_supplier")
private Supplier supplier;
private String description;
public Supplier getSupplier()
{
return supplier;
}
}
And
#Entity
public class Supplier
{
#Id
private Long supplierID;
#OneToMany(mappedBy = "supplier")
#JsonBackReference
private Set<Product> products = new HashSet<>();
private String name;
public Set<Product> getProducts()
{
return products;
}
}
With those definitions, I get the results back that I want if I query for a product. For example like this:
{
"productID": 1,
"supplier": {
"supplierID": 1,
"name": "Acer"
},
"description": "Gaming chair"
}
But if I do it the other way round, thus querying a supplier, I only get this back:
{
"supplierID": 1,
"name": "Acer"
}
Here I am missing the products.
How can this be achieved?
OneToMany is inherently lazy so if you want to load Supplier and all products you need to eagerly fetch the products
#OneToMany(fetch = FetchType.EAGER)
// or
#Query("select s from Supplier s left join fetch s.products where s.supplierID = 1)
Also as you have a circular dependency between supplier and product you have used #JsonBackReference on the products collection which will prevent the products being serialised to json, hence your response
If you want to return Supplier with list of products or a product with the supplier and prevent circular dependency issues remove #JsonBackReference and #JsonManagedReference reference and use #JsonIgnoreProperties instead
#JsonIgnoreProperties("products")
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "fk_supplier")
private Supplier supplier;
#JsonIgnoreProperties("supplier")
#OneToMany(mappedBy = "supplier", fetch = FetchType.EAGER)
private Set<Product> products = new HashSet<>();

JSON don't repeat some attributes and respective values

for all instances serialized, from the second occurrence onwards, of the same model class, the objects only have a part of the attributes and their respective values ​​that they should. This way the JSON file structure is not homogeneous and uniform as expected by the old part of the application (Frontend).
[
{
"id": 1,
"fornecedor": {
"cnpj": "80000876000177",
"nome": "ATACADÃO DIA-A-DIA"
},
"itens": [
{
"id": 2,
"produto": {
"gtin": "7891991010856",
"nome": "CERVEJA BUDWEISER"
},
"quantidade": 3,
"preco": 3.5,
"total": 10.5
}
]
},
{
"id": 3,
"fornecedor": {
"cnpj": "19600347000101",
"nome": "SUPERMERCADO BELLA-VIA"
},
"itens": [
{
"id": 4,
"produto": {
"gtin": "7891991010857",
"nome": "CERVEJA HEINEKEN"
},
"quantidade": 4,
"preco": 5.4,
"total": 21.6
}
]
},
{
"id": 5,
"fornecedor": "19600347000101",
"itens": [
{
"id": 6,
"produto": "7891991010856",
"quantidade": 4,
"preco": 3.2,
"total": 12.8
}
]
},
{
"id": 7,
"fornecedor": "80000876000177",
"itens": [
{
"id": 8,
"produto": "7891991010857",
"quantidade": 5,
"preco": 4.9,
"total": 24.5
}
]
}
]
In the JSON above, the instances with id:1 and id:7 have a field named fornecedor that is structurally different and that shouldn't be because the Frontend expects them to be the same. Other samples are id:2 and id:6 with the field produto and so on.
The JSON above is the serialization of the list of model class Pedido below:
#Data
#Entity
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Pedido {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JsonView(View.External.class)
private Long id;
#ManyToOne
#JoinColumn(name = "fornecedor_cnpj")
#JsonView(View.External.class)
private Fornecedor fornecedor;
#OneToMany(mappedBy = "pedido", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
#JsonView(View.External.class)
private List<Item> itens;
public List<Item> getItens() {
if(itens == null){
itens = new ArrayList<>();
}
return itens;
}
}
The model class Fornecedor is:
#Data
#Entity
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "cnpj")
public class Fornecedor {
#Id
#JsonView(View.External.class)
private String cnpj;
#JsonView(View.External.class)
private String nome;
#OneToMany(mappedBy = "fornecedor", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
#JsonView(View.Internal.class)
private List<Pedido> pedidos;
#OneToMany(mappedBy = "fornecedor", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
#JsonView(View.Internal.class)
private List<Disponibilidade> disponilidades;
}
And the model class Produto is:
#Entity
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "gtin")
#NoArgsConstructor
#Data
public class Produto {
#Id
#JsonView(View.External.class)
private String gtin;
#JsonView(View.External.class)
private String nome;
#OneToMany(mappedBy = "produto", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
#JsonView(View.Internal.class)
private List<Disponibilidade> disponibilidades;
public Produto(String gtin) {
this.gtin = gtin;
}
}
Question: The structure of JSON is the representation of expected behavior? If so, could someone tell me how do I make all instances from the first one to always have all attributes with their respective values ​​(as much as that means repeated attributes and values!).
Thanks!
The JSON you are getting is a consequence of the usage of #JsonIdentityInfo that you or someone else may have added to solve an infinite recursion while serializing your Java objects to JSON (you can read more about this at https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion). This means that the only way to have the complete set of attributes in fornecedor and produto is to get rid of #JsonIdentityInfo on those classes. Keep in mind that this might create the infinite recursion problem mentioned above.
#Data
#Entity
public class Fornecedor {
(...)
}
#Entity
#NoArgsConstructor
#Data
public class Produto {
(...)
}

Spring Data, MySQL and not working unique value

I'm having troubles getting into Spring Data
I got entity Product which has Category (I'm guessing relation type is right? Product has one Category, Category has many products)
#Entity
public class Product implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "category_id")
private Category category;
}
#Entity
class Category implements Serializable {
public Category() {
}
public Category(String name){
this.name = name;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "category_id")
private Long id;
#Column(unique = true)
private String name;
#OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Product> products;
}
Now I try to add new Product via Postman, calling my RestController
#PostMapping("/add")
public Product addProduct(#Valid #RequestBody Product product){
return repository.save(product);
}
With 2 following requests
{
"name" : "pork",
"category" : "meat"
}
{
"name" : "chicken",
"category" : "meat"
}
In the result I got 2 following responses
{
"id": 1,
"name": "pork",
"category": {
"id": 1,
"name": "meat",
"products": null
}
}
{
"id": 2,
"name": "chicken",
"category": {
"id": 2,
"name": "meat",
"products": null
}
}
And on database I actually got 2 categories named "meat" (even tho it should be unique. What's more, do I actually need Set<Product> in my Category class? TBH, Category has no intrest in that at all.
There are a few problems with your code.
You are directly using entity as the rest API model. Suggest to create a separate ProductModel with only fields that client has access to.
You mixing category creation together inside product creation, but your category in the request only contains name. To the backend, unless you check whether such a category exists, it's always treated as a new category.
Before you call repository.save, you need let category knows what's the product inside. In your current code, only product know its category.
You don't need Set products in your Category class (and it's recommended to use only #ManyToOne).

How to access spring jpa references in json

I have two entitys, A and B. Lets say, that, A has some fields (name, location). B has some fields too + a #ManyToOne relationship to A.
Now if I run my app, I can see the entitys and their valus in ...myDomain/api and specific entitys in ...myDomain/api/A for example. Now if look at ...myDomain/api/B/1, I can see B-s values + under _links a reference to A. How can I get A to already be as a value in B, not a link.
End result should look smth like this:
{
"_embedded" : {
"B" : [ {
"id" : 1,
"someData" : "data",
"otherData" : "other",
"A" : {
"name": "myName",
"location": "myLoc"
} ]
UPDATE
#Entity
#Data //lombok
public class A extends SuperClass{
private String name;
private String location;
}
#Entity
#Data
public class B extends SuperClass {
private String someData;
private String otherData;
#ManyToOne(optional = false, cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private A a;
}
public class SuperClass implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
}
Both entitys have a simple repository interface which extend CrudRepository.
UPDATE II
Now if I add #RestResourece( exported = false ) tag after #ManyToOne tag, I get the A entity "exposed" and I can access the data. But now, doing a POST on my B entity, I can't access it anymore because B isn't found by Resource<B> anymore. Why is that so?

Retrieving details from multiple tables using SpringMVC

I am developing a Shopping Application, In my Application i have table tenant and in tenant table i have have column Binary_id which is primary key in binary table in database. Now when i making a get request to tenant table i am getting all the tenant table fields as JSON. But I have #ManyToOne relation from binary table to tenant i.e tenant can have multiple records in binary. So, while making GET call from POSTMAN client instead of getting tenant details, i need to get all the binary records related to that tenant as JSON.
Now i am getting JSON as follows when making a call to http://localhost:8080/sportsmvc/rest/tenant from POSTMAN Client
[
{
"id": 2,
"binaryId": "1002",
"name": "AltisArena"
},
{
"id": 9,
"binaryId": "1001",
"name": "Agon"
}
]
But i need the responce JSON As below:
[
{
"id": 2,
"name": "AltisArena",
"listOfBinary": [
{
"tenant_id": 2,
"location": "location1",
"description": "ABC"
},
{
"tenant_id": 2,
"location": "location2",
"description": "ABCD"
}
]
},
{
"id": 9,
"name": "Agon",
"listOfBinary": [
{
"tenant_id": 9,
"location": "location3",
"description": "desc1"
},
{
"tenant_id": 9,
"location": "location4",
"description": "desc2"
}
]
}
]
Code snippets:
Tenant Entity:
#Entity
#Table(name="tenant", catalog="db_sports" )
// Define named queries here
#NamedQueries ( {
#NamedQuery ( name="TenantEntity.countAll", query="SELECT COUNT(x) FROM TenantEntity x" )
} )
public class TenantEntity implements Serializable {
private static final long serialVersionUID = 1L;
//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="id", nullable=false)
private Integer id ;
#Column(name="name", nullable=false, length=300)
private String name ;
//----------------------------------------------------------------------
// ENTITY LINKS ( RELATIONSHIP )
//----------------------------------------------------------------------
#ManyToOne
#JoinColumn(name="binary_id", referencedColumnName="id")
private SwaBinaryEntity swaBinary ;
SWA_Binary Entity:
#Entity
#Table(name="SWA_Binary", catalog="db_sports" )
// Define named queries here
#NamedQueries ( {
#NamedQuery ( name="SwaBinaryEntity.countAll", query="SELECT COUNT(x) FROM SwaBinaryEntity x" )
} )
public class SwaBinaryEntity implements Serializable {
private static final long serialVersionUID = 1L;
//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
#Id
#Column(name="id", nullable=false, length=100)
private String id ;
#Column(name="file_location", nullable=false, length=400)
private String fileLocation ;
#Column(name="description", nullable=false, length=200)
private String description ;
TenantRestController:
#RequestMapping( value="/tenant",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public List<Tenant> findAll() {
return tenantService.findAll();
}
TenantServiceImpl:
#Override
public List<Tenant> findAll() {
List<TenantEntity> entities = tenantPersistence.loadAll();
List<Tenant> beans = new ArrayList<Tenant>();
for(TenantEntity entity : entities) {
beans.add(tenantServiceMapper.mapTenantEntityToTenant(entity));
}
return beans;
}
TenantServiceMapper:
public Tenant mapTenantEntityToTenant(TenantEntity tenantEntity) {
if(tenantEntity == null) {
return null;
}
//--- Generic mapping
Tenant tenant = map(tenantEntity, Tenant.class);
//--- Link mapping ( link to SwaBinary )
if(tenantEntity.getSwaBinary() != null) {
tenant.setBinaryId(tenantEntity.getSwaBinary().getId());
}
return tenant;
}
Can anyone please help to to solve this issue.
Thanks in Advance.
Maybe I misunderstood model, but it seems like it's a bit wrong. In what you need JSON you have tenant that have multiple binaries, but in JPA model it's vise versa and tenant have 1 binary.
In TenantEntity shouldn't it be like this ?:
#OneToMany
private List<SwaBinaryEntity> swaBinary