How to map the entity class object in springboot? - java

This is my entity class , which I am using to save the details of customer along with the purchase detail and product purchased and payment module , Its finely saving in DB but while retrieving getting result as null .
#Entity
#Table(name = "productpurchasedata")
public class Test {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int customerid;
#ManyToOne(cascade = { CascadeType.ALL })
#JoinColumn(name = "customerid", insertable = false, updatable = false)
private CustomerProductSave customerProductSave;
#ManyToOne(cascade = { CascadeType.ALL })
#JoinColumn(name = "customerid", insertable = false, updatable = false)
private PurchaseDetails purchasedetails;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name = "customerid")
private List<ProductPurchased> productpurchased;
#ManyToOne(cascade = { CascadeType.ALL })
#JoinColumn(name = "customerid", insertable = false, updatable = false)
private PaymentHistory paymenthistory;
Here I am able to save the data which result in something like this :-
{
"customerid": 1,
"customerProductSave": {
"customerids": 2,
"name": "nitesh",
"mailid": "niteshbhushan95#gmail.com",
"address": "hyd",
"city": "telanagana",
"state": "telanagana",
"postalcode": "847226",
"mobileno": 6300769631
},
"purchasedetails": {
"purchasedate": "22-07-2021",
"modeofpayment": "paytm",
"emiapplicable": false,
"totalamount": 21000,
"purchaseId": 4
},
"productpurchased": [
{
"purchaseid": 5,
"purchaseamount": 2000,
"purchasedproductid": 1,
"productquantity": 0
},
{
"purchaseid": 6,
"purchaseamount": 2000,
"purchasedproductid": 1,
"productquantity": 0
}
],
"paymenthistory": {
"transactionid": 3,
"paymentstatus": "paid"
}
}
Now when I am trying to retrieve the data by customerid it is not accepting , it giving message as null , I am not pretty much may be some mistake in mapping

try using fetch = FetchType.EAGER instead of fetch = FetchType.LAZY at all mappings
purchase detail and product purchased and payment module

Related

Hibernate is deleting nested relationships when editing parent

i have this four classes:
#Entity
#Table(name = "products")
public class Product extends RegionDiscriminator {
#Column(nullable = false, name = "product_id")
private Long productId;
#Column(nullable = false, name = "name")
private String name;
#Column(nullable = false, name = "category_id")
private Long categoryId;
#Column(nullable = false, name = "unit")
private String unit;
#OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SkuGroup> skuGroups = new ArrayList<>();
#OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Tax> taxes = new ArrayList<>();
#Column(nullable = false, name = "active")
private boolean active;
}
#Entity
#Table(name = "sku_groups")
public class SkuGroup extends RegionDiscriminator {
#Column(nullable = false, name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "product_id")
private Product product;
#OneToMany(mappedBy = "skuGroup", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Sku> skus = new ArrayList<>();
#OneToMany(mappedBy = "skuGroup", cascade = CascadeType.ALL, orphanRemoval = true)
#OrderBy("period DESC")
private List<Cluster> clusters = new ArrayList<>();
}
#Entity
#Table(name = "skus")
public class Sku extends GrowthEntity {
#Column(nullable = false, name = "sku_id")
private Long skuId;
#Column(nullable = false, name = "name")
private String name;
#Column(nullable = false, name = "min_weight_unit")
private BigDecimal minWeightUnit;
#Column(name = "upc")
private String upc;
#OneToMany(mappedBy = "sku", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Tier> tiers = new ArrayList<>();
#OneToMany(mappedBy = "sku", cascade = CascadeType.ALL, orphanRemoval = true)
#Size(max = 10)
#OrderBy("creationTimestamp DESC")
private List<Price> prices = new ArrayList<>();
#ManyToOne
#JoinColumn(name = "sku_group_id")
private SkuGroup skuGroup;
#OneToMany(mappedBy = "sku", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Discount> discounts = new ArrayList<>();
#Column(nullable = false, name = "active")
private boolean active;
#OneToOne(mappedBy = "sku")
private SkuCost cost;
#OneToOne(mappedBy = "sku")
private SkuBenchmark benchmark;
#OneToMany(mappedBy = "sku", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<PricingProcessDetail> pricingProcessDetails;
}
#Entity
#Table(name = "prices")
public class Price extends GrowthEntity {
#Column(nullable = false, name = "retail_price")
private BigDecimal retailPrice;
#Column(nullable = false, name = "tax_price")
private BigDecimal taxPrice;
#Column(nullable = false, name = "pricing_price")
private BigDecimal pricingPrice;
#Column(nullable = false, name = "sale_price")
private BigDecimal salePrice;
#ManyToOne
#JoinColumn(name = "sku_id")
private Sku sku;
#ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
#JoinTable(name = "price_discount",
joinColumns = #JoinColumn(name = "price_id"),
inverseJoinColumns = #JoinColumn(name = "discount_id")
)
private List<Discount> discounts = new ArrayList<>();
#ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
#JoinTable(name = "price_tax",
joinColumns = #JoinColumn(name = "price_id"),
inverseJoinColumns = #JoinColumn(name = "tax_id")
)
private List<Tax> taxes = new ArrayList<>();
#OneToMany(mappedBy = "price", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PriceTier> tiers = new ArrayList<>();
}
This are the methods executed by my service:
#Transactional
public Product updateProduct(final ProductDTO productDTO) {
final Product product = productDao.findByProductId(productDTO.getProductId())
.orElseThrow(() -> new GrowthNotFoundException(format("Product with id %s not found",
productDTO.getProductId())));
product.update(productDTO);
return save(product);
}
public Product update(final ProductDTO productDTO) {
categoryId = productDTO.getCategory() != null ? productDTO.getCategory().getId() : null;
name = productDTO.getName();
unit = productDTO.getUnit();
active = productDTO.isActive();
this.updateTaxes(productDTO);
this.updateSkuGroups(productDTO);
return this;
}
private void updateTaxes(final ProductDTO productDTO) {
List<Tax> newTaxes = buildTaxes(productDTO);
List<Tax> currentTaxes = new ArrayList<>(taxes);
if (!currentTaxes.equals(newTaxes)) {
taxes.clear();
taxes.addAll(newTaxes);
}
}
private void updateSkuGroups(final ProductDTO productDTO) {
Map<Long, Sku> skusMap = allSkus().stream()
.collect(Collectors.toMap(Sku::getSkuId, Function.identity()));
Map<String, List<SkuDTO>> newSkuGroups = ProductDTO.groupSkus(productDTO.getSkus());
Map<String, SkuGroup> currentSkuGroups = skuGroups.stream().collect(
Collectors.toMap(SkuGroup::getName, skuGroup -> skuGroup));
newSkuGroups.forEach((group, skus) -> {
if (currentSkuGroups.containsKey(group)) {
SkuGroup skuGroup = currentSkuGroups.get(group);
skuGroup.update(
skus.stream().map(skuDTO -> createOrUpdateSku(skusMap, skuDTO, skuGroup))
.collect(Collectors.toList()));
} else {
SkuGroup skuGroup = SkuGroup.create(group, this);
skuGroup.update(
skus.stream().map(skuDTO -> createOrUpdateSku(skusMap, skuDTO, skuGroup))
.collect(Collectors.toList()));
skuGroups.add(skuGroup);
}
});
currentSkuGroups.forEach((group, skuGroup) -> {
if (!newSkuGroups.containsKey(group)) {
skuGroups.remove(skuGroup);
}
});
}
private Sku createOrUpdateSku(Map<Long, Sku> skusMap, SkuDTO skuDTO, SkuGroup skuGroup) {
if (skusMap.containsKey(skuDTO.getId())) {
return skusMap.get(skuDTO.getId()).update(skuDTO, skuGroup);
} else {
return Sku.create(skuDTO, skuGroup);
}
}
public static Sku create(final SkuDTO skuDTO, final SkuGroup skuGroup) {
Sku sku = Sku.builder()
.withSkuId(skuDTO.getId())
.withMinWeightUnit(skuDTO.getMinWeightUnit())
.withUpc(skuDTO.getUpc())
.withName(skuDTO.getName())
.withSkuGroup(skuGroup)
.isActive(skuDTO.isActive())
.build();
sku.createTiers(skuDTO.getTiers(), skuDTO.getPrice());
sku.createPrice(skuDTO);
return sku;
}
public Sku update(final SkuDTO skuDTO, final SkuGroup theSkuGroup) {
minWeightUnit = skuDTO.getMinWeightUnit();
upc = skuDTO.getUpc();
name = skuDTO.getName();
skuGroup = theSkuGroup;
active = skuDTO.isActive();
this.updateTiers(skuDTO.getTiers(), skuDTO.getPrice());
this.updatePriceFromRetailPrice(skuDTO.getPrice());
return this;
}
This is an example of ProductDTO:
{
"productId": 16825,
"name": "Producto prueba pricing 30",
"category": {
"id": 100515,
"name": "Categoria prueba 13"
},
"iva": null,
"unit": "UNID",
"activeStartDate": "2022-11-22 12:18:08",
"activeEndDate": "2022-11-22 12:18:08",
"skus": [
{
"id": 22937,
"upc": "BOG-FRU1-CAT4252-16825:17883:17882:22937",
"name": "Producto prueba pricing 30 (Caja x 12) - 30 unidades 160g",
"owner": {
"id": 1,
"name": "Frubana"
},
"price": 999,
"stepUnit": 1,
"minWeightUnit": 1,
"conversion": {
"conversionTypeX": "UNID",
"conversionValueX": null,
"conversionTypeY": null,
"conversionValueY": null
},
"slot": false,
"cooled": false,
"activeStartDate": "2022-10-19 12:17:04",
"activeEndDate": "2022-10-19 12:17:04",
"tiers": null,
"group": "(Caja x 24)",
"active": true
},
{
"id": 22938,
"upc": "BOG-FRU1-CAT4252-16825:17885:17884:22938",
"name": "Producto prueba pricing 30 (Caja x 24) - 30 unidades de 150g",
"owner": {
"id": 1,
"name": "Frubana"
},
"price": 888,
"stepUnit": 1,
"minWeightUnit": 1,
"conversion": {
"conversionTypeX": "UNID",
"conversionValueX": null,
"conversionTypeY": null,
"conversionValueY": null
},
"slot": false,
"cooled": false,
"activeStartDate": "2022-10-19 12:17:04",
"activeEndDate": "2022-10-19 12:17:04",
"tiers": null,
"group": "(Caja x 12)",
"active": true
},
{
"id": 22939,
"upc": "BOG-FRU1-CAT4252-16825:17887:17886:22939",
"name": "Producto prueba pricing 30 (Caja x 24) - 30 unidades de 160g",
"owner": {
"id": 1,
"name": "Frubana"
},
"price": 777,
"stepUnit": 1,
"minWeightUnit": 2,
"conversion": {
"conversionTypeX": "UNID",
"conversionValueX": null,
"conversionTypeY": null,
"conversionValueY": null
},
"slot": false,
"cooled": false,
"activeStartDate": "2022-10-19 12:17:04",
"activeEndDate": "2022-10-19 12:17:04",
"tiers": null,
"group": "(Caja x 24)",
"active": true
}
],
"image": "/cmsstatic/products/sku_sin_imagen.png",
"active": true
}
My goal is to assign the SKUs to their respective SKU Groups based on the name, eliminating the groups that are left empty and creating the new ones that are necessary.
The code works for the first part, but fails when the reassignment needs the creation of a new group.
ERROR: null value in column "sku_id" violates not-null constraint

How to control fetched sub entities spring boot

I have following relationship in my spring boot:
public class Clazz {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id")
private Integer id;
#Column(name = "lesson_id")
#NotNull(message = "{lesson.is_required}")
private Integer lessonId;
#Column(name = "level_id")
#NotNull(message = "{level.is_required}")
private Integer levelId;
#Column(name = "name")
#NotEmpty(message = "{name.is_required}")
private String name;
#OneToOne
#JoinColumn(name = "lesson_id", referencedColumnName = "id", insertable = false, updatable = false)
private Lesson lesson;
#OneToOne
#JoinColumn(name = "level_id", referencedColumnName = "id", insertable = false, updatable = false)
private Level level;
}
Now my Lesson entity has Level entity:
public class Lesson {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "name")
#NotEmpty(message = "{name.is_required}")
private String name;
#Column(name = "description")
private String description = "";
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "lesson_id")
Collection<Level> levels = new ArrayList<>();
}
And finally my Level class:
public class Level {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id", insertable = false, updatable = false)
private Integer id;
#Column(name = "lesson_id")
private Integer lessonId;
#Column(name = "name")
private String name;
#Column(name = "step")
private Integer step;
#Column(name = "description")
private String description="";
}
With this relationship defined, when I fetch data, getting some extra info in my results:
[
{
"id": 2,
"lessonId": 1,
"levelId": 1,
"name": "English Class March",
"branch": {
"id": 1,
"name": "3 mikr branch",
"email": "",
"phone": "",
"address": "3 mikr, foo, bar"
},
"lesson": {
"id": 1,
"name": "math",
"description": "math lesson",
"levels": [
{
"id": 1,
"lessonId": 1,
"name": "First level",
"step": 1,
"description": "1st level descr"
}
]
},
"level": {
"id": 1,
"lessonId": 1,
"name": "First level",
"step": 1,
"description": "1st level descr"
}
}
]
How can I tell JPA not to fetch sub-entities, ideally I do not need levels in lesson field of result.
Use #JsonIgnore. It can be used at setter,getter or field.
Please refer the code below.
public class Lesson {
#Id #Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "name")
#NotEmpty(message = "{name.is_required}")
private String name;
#Column(name = "description")
private String description = "";
#JsonIgnore // Add this to ignore the property in json output
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "lesson_id")
Collection<Level> levels = new ArrayList<>();
}

Violation of PRIMARY KEY With User Registration in Authorization Server

A simple application is created by using oauth2 password flow. The Authorization Server is working as the identity provider and when a new user is registered then the jwt token should be received as a response.
Relationships between entities
User (M) ------------------ Role (M)
Role (M) ------------------ Permission (M)
when I signup with a new user the following error has thrown
Caused by: java.sql.SQLSyntaxErrorException: ORA-02275: such a referential constraint already exists in the table
How can I resolve the primary key violation with JPA? If I use Native Hibernate API then we can use session.merge() but in JPA there is no option like that.
Request
{
"userName": "Nafaz Benzema",
"password": "stackoverflow",
"email": "benz#gmail.com",
"active": "y",
"accNonExpired": "y",
"credentialNonExpired": "y",
"accNonLocked": "y",
"roles" : [
{
"id": 101,
"name": "ROLE_USER",
"permissions": [
{
"id": 10,
"name": "CAN_CREATE"
},
{
"id": 40,
"name": "CAN_READ"
}
]
},
{
"id": 102,
"name": "ROLE_ADMIN",
"permissions": [
{
"id": 10,
"name": "CAN_CREATE"
},
{
"id": 40,
"name": "CAN_READ"
},
{
"id": 20,
"name": "CAN_UPDATE"
},
{
"id": 40,
"name": "CAN_DELETE"
}
]
}
]
}
Entity classes
User class
#Entity
#Table(name = "USER90",schema = Schema.TESTDB,uniqueConstraints = {
#UniqueConstraint(name = "userName",columnNames = "USER_NAME"),
#UniqueConstraint(name = "email",columnNames = "EMAIL")
})
#Getter
#Setter
public class User {
#Id
#SequenceGenerator(name = "USER_ID_GEN",sequenceName = Schema.TESTDB+".USER_ID_SEQ",initialValue = 1003,allocationSize = 1)
#GeneratedValue(generator = "USER_ID_GEN",strategy = GenerationType.SEQUENCE)
#Column(name = "USER_ID")
private int userId;
#Column(name = "USER_NAME",nullable = false)
private String userName;
#Column(name = "PASSWORD",nullable = false)
private String password;
#Column(name = "EMAIL",nullable = false)
private String email;
#Column(name = "ACTIVE",nullable = false)
private String active;
#Column(name = "ACC_NON_EXPIRED")
private String accNonExpired;
#Column(name = "CREDENTIAL_NON_EXPIRED")
private String credentialNonExpired;
#Column(name = "ACC_NON_LOCKED")
private String accNonLocked;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "USER_ROLE",joinColumns = {#JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")},
inverseJoinColumns = {#JoinColumn(name = "ROLE_ID",referencedColumnName = "ID")})
private Set<Role> roles;
}
Role class
#Entity
#Table(name = "ROLE",schema = Schema.TESTDB,uniqueConstraints = {
#UniqueConstraint(name = "name",columnNames = "NAME")
})
#Getter
#Setter
public class Role {
#Id
#Column(name = "ID")
private long id;
#Enumerated(EnumType.STRING)
#Column(name = "NAME")
private ERole name;
// bi-directional
/* #ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<User> users;*/
// uni-directional
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "PERMISSION_ROLE",joinColumns = {#JoinColumn(name = "ROLE_ID",referencedColumnName = "ID")},
inverseJoinColumns = {#JoinColumn(name = "PERMISSION_ID",referencedColumnName = "ID")})
private Set<Permission> permissions;
}
Permission class
#Entity
#Table(name = "PERMISSION",schema = Schema.TESTDB,uniqueConstraints = {
#UniqueConstraint(name = "name",columnNames = "NAME")
})
#Getter
#Setter
public class Permission {
#Id
#Column(name = "ID")
private long id;
#Enumerated(EnumType.STRING)
#Column(name = "NAME")
private EPermission name;
}
Service class
public Response userRegistration(SignupRequest signup) {
if(!Objects.isNull(userDAO.findUserByEmail(signup.getEmail()).orElse(null)))
throw new UserIsExistedException(String.format("User is existed with %s",signup.getEmail()));
try {
User user=new User();
String password="{bcrypt}";
password = password.concat(BCrypt.hashpw(signup.getPassword(),BCrypt.gensalt(12)));
user.setUserName(signup.getUserName());
user.setEmail(signup.getEmail());
user.setPassword(password);
user.setActive("y");
user.setAccNonExpired("y");
user.setCredentialNonExpired("y");
user.setAccNonLocked("y");
user.setRoles(signup.getRoles());
Set<Role> roles = new HashSet<>();
user.getRoles().forEach(role->{
roles.add(role);
Set<Permission> permissions=new HashSet<>();
role.getPermissions().forEach(perm->{
permissions.add(perm);
});
role.setPermissions(permissions);
});
user.setRoles(roles);
userDAO.save(user);
LOGGER.info("user is saved and response is returned successfully");
return new Response(user.getEmail(), authenticationProvider.obtainToken(user.getEmail(), user.getPassword()).toString());
}catch (NumberFormatException ex){
LOGGER.error("NumberFormat Exception");
throw new NumberFormatException("NumberFormat Exception");
}
catch (Exception ex){
LOGGER.error("invalid username or password");
throw new BadCredentialsException("invalid username or password",ex);
}
}
Note - If you need more resource here GitHub link
github_link
config file
spring:
datasource:
url: jdbc:oracle:thin:#localhost:1521:orcl
username: TESTDB
password: 14292
driver-class-name: oracle.jdbc.OracleDriver
jpa:
database-platform: org.hibernate.dialect.Oracle10gDialect
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: update
I have solved it by changing the CascadeType.
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "USER_ROLE",joinColumns = {#JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")},
inverseJoinColumns = {#JoinColumn(name = "ROLE_ID",referencedColumnName = "ID")})
private Set<Role> roles;
when I use CascadeType.ALL then it gives priority to PERSIST (save) which throws Primary Key Violation in my case. When roles assigned to the user and if it is available in the role table then I need to Merge it rather than Save. So I have changed the CascadeType from ALL to MERGE.
#ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
#JoinTable(name = "USER_ROLE",joinColumns = {#JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")},
inverseJoinColumns = {#JoinColumn(name = "ROLE_ID",referencedColumnName = "ID")})
private Set<Role> roles;

Spring REST deserialize JSON array as a Set<?>

I'm using Spring Boot 2.2, Spring Data REST, Spring HATEOAS.
I'm facing a strange problem. I've a RestController accepting this object:
#Data
public class DocumentJSON {
#Valid
private Document document;
private List<DocumentRow> rows = new ArrayList<>();
private List<DocumentVat> vats = new ArrayList<>();
private Set<DocumentPayment> payments = new HashSet<>();
private boolean updateContactDetail = false;
}
and DocumentPayment is:
#Data
#EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
#NoArgsConstructor
#AllArgsConstructor
#Builder
#ToString(callSuper = true)
public class DocumentPayment extends AbstractEntity {
#ToString.Exclude
#JsonDeserialize(using = DocumentUriDeserializer.class)
#NotNull
#OnDelete(action = OnDeleteAction.CASCADE)
#ManyToOne(fetch = FetchType.LAZY, optional = false)
private Document document;
#NotNull
#Column(nullable = false, columnDefinition = "DATE")
private Instant date;
//Optional contact (the receipt has not a contact)
#ToString.Exclude
#ManyToOne(fetch = FetchType.LAZY)
private Contact contact;
#NotBlank
#Column(nullable = false)
private String description;
#ToString.Exclude
#JsonDeserialize(using = FinancialAccountUriDeserializer.class)
#NotNull(message = "{documentpayment.financialaccount.missing}")
#ManyToOne(fetch = FetchType.LAZY, optional = false)
private FinancialAccount financialAccount;
#Enumerated(EnumType.STRING)
#NotNull
#Column(nullable = false, length = 30)
private PaymentType paymentType;
//The amount, negative for payment to suppliers
#NotNull
#ColumnDefault("0.00")
#Column(nullable = false, scale = 2, columnDefinition = "DECIMAL(12,2)")
private BigDecimal amount = BigDecimal.ZERO;
//The amount paid
#NotNull
#ColumnDefault("0.00")
#Column(nullable = false, columnDefinition = "DECIMAL(12,2)")
private BigDecimal paid = BigDecimal.ZERO;
#JsonProperty(access = JsonProperty.Access.READ_ONLY)
#Generated(value = GenerationTime.ALWAYS)
#Column(columnDefinition = "DECIMAL(12,2) AS (amount-paid) VIRTUAL NOT NULL")
private BigDecimal due;
#ToString.Exclude
#JsonDeserialize(using = StoreUriDeserializer.class)
//#NotNull(message = "{documentpayment.store.missing}")
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "store_id", updatable = false)
private Store store;
}
The client is sending a Json like this:
{
"document": {
"date": "2019-10-18T00:00:00.000Z",
"type": "SALES_RECEIPT",
"store": "http://95.255.117.252:8082/api/v1/stores/1",
"rounding": 0,
"amount": 19.23,
"taxAmount": 0.77,
"totalAmount": 20
},
"rows": [
{
"index": 1,
"productType": "FRAME",
"qty": 1,
"rowGroup": null,
"unitPrice": 9.615,
"percentageDiscount": 0,
"purchaseUnitPrice": null,
"amount": 9.615,
"description": "Prodotto1",
"taxRate": "http://95.255.117.252:8082/api/v1/taxRates/2",
"note": false
},
{
"index": 1,
"productType": "OPHTHALMIC_LENS",
"qty": 1,
"rowGroup": null,
"unitPrice": 9.615,
"percentageDiscount": 0,
"purchaseUnitPrice": null,
"amount": 9.615,
"description": "Lente",
"taxRate": "http://95.255.117.252:8082/api/v1/taxRates/2",
"note": false
}
],
"payments": [
{
"date": "2019-10-18T00:00:00.000Z",
"financialAccount": "http://95.255.117.252:8082/api/v1/financialAccounts/1",
"paymentType": "CASH",
"amount": "10"
},
{
"date": "2019-10-18T00:00:00.000Z",
"financialAccount": "http://95.255.117.252:8082/api/v1/financialAccounts/3",
"paymentType": "CREDIT_CARD",
"amount": "10"
}
],
"updateContactDetail": false
}
but when I debug in the first line of the REST controller I see only 1 payment.
Changing the property payments in DocumentJSON from Set<DocumentPayment> to List<DocumentPayment>, I get 2 payments as expected with the same JSON.
Am I missing something or Spring/Jackson are not able to deserialize in the right way a Set<>?

Swagger 2.0 + Jackson: #JsonIdentityInfo doesn't work

We are attempting to have Swagger 2.0. Basically, it's great except it is ignoring the #JsonIdentityInfo and #JsonIdentityReference annotations.
public class Source {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne
#JsonIdentityReference(alwaysAsId=true)
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
#JsonProperty("sourceDefinitionName")
private SourceDefinition sourceDefinition;
... getters and setters
}
Resulting in the Swagger schema output:
{
"id": 0,
"sourceDefinitionName": {
"configuration": {},
"driver": "string",
"id": "string",
"name": "string",
"sourceType": "QUERY",
"title": "string"
}
}
You can see that it indeed reads the #JsonProperty annotation renaming the "sourceDefinition" to "sourceDefinitionName" but the value should just be a string.
Does anyone have any insight into this sort of problem with this integration?
#ManyToOne(targetEntity = Source.class, optional = false)
#JoinColumn(name = "SOURCE_DEF_NAME", referencedColumnName = "name", nullable = false,
foreignKey = #ForeignKey(name = "FK_sourceDefName"))
private SourceDefinition sourceDefinition;
#Column(name = "SOURCE_DEF_NAME", insertable = false, updatable = false)
private String sourceDefinitionName;
Json identity is not working for me too. I have fond following workaround. see working example json-sample

Categories

Resources