OutOfMemory Error : GC overhead limit exceeded - Hibernate - java

I am using hibernate to save data to my table.
I have my entity class and main class, through main class i have called the entity class constructor and build the object and in a for loop saving the object to DB through hibernate. I am getting OutofMemory Error : GC overhead limit exceeded, I dont understand why, Can anyone please help?
OutOfMemoryError
Here's my code :
Session session = HibernateSessionFactory.getSession();
for(int i=0;i<serviceIds.length;i=i++)
{
EntityClass ec = new EntityClass
(Integer.parseInt(serviceIds[i]),0,someId3, 0,1,id2,
new Timestamp(System.currentTimeMillis()), 0,
null, null, 0, null,null,null,null);
session.save(ec);
}
session.flush();
session.clear();
Here's my entity class:
public class EntityClass implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer someId1;
private Integer someId2;
private Integer someId3;
private Integer flag1;
private Integer flag2;
private Integer createdBy;
private Timestamp createdDate;
private Integer modifiedBy;
private Timestamp modifiedDate;
private Timestamp endDate;
private Integer attribute1;
private String attribute2;
private String attribute3;
private String attribute4;
private String attribute5;
//full constructor
public EntityClass(Integer someId1, Integer someId2,
Integer someId3, Integer funBlockFlag, Integer functionalFlag,
Integer createdBy, Timestamp createdDate, Integer modifiedBy,
Timestamp modifiedDate, Timestamp endDate, Integer attribute1,
String attribute2, String attribute3, String attribute4,
String attribute5) {
this.someId1= someId1;
this.someId2 = someId2;
this.someId3 = someId3;
this.funBlockFlag = funBlockFlag;
this.functionalFlag = functionalFlag;
this.createdBy = createdBy;
this.createdDate = createdDate;
this.modifiedBy = modifiedBy;
this.modifiedDate = modifiedDate;
this.endDate = endDate;
this.attribute1 = attribute1;
this.attribute2 = attribute2;
this.attribute3 = attribute3;
this.attribute4 = attribute4;
this.attribute5 = attribute5;
}
//getter and setters of all fields
Can anyone help me with the issue?

When we persist an entity (in this case EntityClass), Hibernate will store it in the persistence context.
From your case, it seem like the length for serviceIds might be too large with the memory that you set for your JVM.
Perhaps try to flush and clear the persistence context for every N elements. For example, let said your BATCH_SIZE is 20
private static final BATCH_SIZE = 20; // declare at class level
Session session = HibernateSessionFactory.getSession();
for(int i=0;i<serviceIds.length;i=i++)
{
EntityClass ec = new EntityClass
(Integer.parseInt(serviceIds[i]),0,someId3, 0,1,id2,
new Timestamp(System.currentTimeMillis()), 0,
null, null, 0, null,null,null,null);
session.save(ec);
if (i % BATCH_SIZE == 0) {
session.flush();
session.clear();
}
}
session.flush();
session.clear();

Related

java.util.NoSuchElementException: No value present

So my problem is in a Test, when I call the method to test it gives this error:
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at com.MD.Medicine.Services.SaveService.savePlans(SaveService.java:57)
at com.MD.Medicine.Services.SaveServiceTest.testSavePlans_failPills(SaveServiceTest.java:99)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
My test:
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SaveServiceTest {
#MockBean
private MedsRepo medsRepo;
#MockBean
private PlansRepo plansRepo;
#MockBean
private PlanDayRepo planDayRepo;
#Autowired
private SaveService saveService;
#Test
void testSavePlans_failPills() {
LocalDate date = LocalDate.now();
Date date3 = new Date(1673740800000L);
Set<PlanDay> setPlans = new HashSet<>();
Plans plans = new Plans(1, setPlans);
BigDecimal price = new BigDecimal(8.00);
Meds meds = new Meds(1, "Brufen", price, "Pain", 200, date, setPlans);
when(medsRepo.getReferenceById(meds.getMedsId())).thenReturn(meds);
int pillNumber = meds.getPillNumber();
List<PlanDay> planList3 = new ArrayList<PlanDay>();
PlanDay planDay3 = new PlanDay(1, date3, "Tuesday", plans, meds, 50000);
planList3.add(planDay3);
String expected3 = saveService.savePlans(planList3);
assertThat(expected3).isEqualTo("Error: No piils available (Existing Pills: " + pillNumber + ")");
}
When it gets in * String expected3 = saveService.savePlans(planList3);* it stops and prints the error.
The method:
public String savePlans(List<PlanDay> plans) throws Error {
//long planIdVerify = plans.get(0).getPlanDaysId();
Date firstDate = plans.get(0).getPlanDate();
long todayMili = System.currentTimeMillis();
long dateLimitMili = firstDate.getTime() + 604800000;
long planId = plans.get(0).getPlans().getPlanId();
Plans plansWithId = new Plans();
plansWithId.setPlanId(planId);
plansRepo.save(plansWithId);
for (int i = 0; i < plans.size(); i++) {
long planDateInMili = plans.get(i).getPlanDate().getTime();
//long planIdMultiVerify = plans.get(i).getPlanDaysId();
if (planDateInMili <= dateLimitMili && todayMili<planDateInMili ) {
PlanDay planDay = plans.get(i);
long medsId = planDay.getMeds().getMedsId();
int medsToTake = planDay.getMedsToTake();
int pillNumber = medsRepo.getReferenceById(medsId).getPillNumber();
int pillUpdate = pillNumber - medsToTake;
Meds updatePlanDay = medsRepo.findById(medsId).get();
if (pillUpdate > 0) {
updatePlanDay.setPillNumber(pillUpdate);
} else {
return "Error: No piils available (Existing Pills: " + pillNumber + ")";
}
planDayRepo.save(planDay);
} else {
return "Week time interval not correct/Invalid planId (only one plan can be saved)";
}
}
return "Saved Successfully";
}
and my entities:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "meds")
#JsonIgnoreProperties(value = { "days" })
public class Meds {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long medsId;
#Column
private String medsName;
#Column
private BigDecimal price;
#Column
private String category;
#Column
private int pillNumber;
#Column
#CreationTimestamp
private LocalDate medsDate;
#OneToMany(mappedBy = "meds", cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY)
#OnDelete(action = OnDeleteAction.CASCADE)
private Set<PlanDay> days = new HashSet<PlanDay>();
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "planDay")
#JsonIgnoreProperties(value = { "planDaysId" })
public class PlanDay {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long planDaysId;
#Column
private Date planDate;
#Column
private String weekday;
#ManyToOne
#JoinColumn(name = "planId", nullable = false)
private Plans plans;
#ManyToOne
#JoinColumn(name = "medsId", nullable = false)
private Meds meds;
#Column
private int medsToTake;
}
I have been looking for a solution and the orElse() method was one of the options but I can't make it work.. What would be a solution for this problem?
Kind Regards.
MedsRepo is a MockBean when you call medsRepo.findById(medsId) it will return an empty optional because you have no when for that method.
int pillNumber = medsRepo.getReferenceById(medsId).getPillNumber(); //extract the Meds as a variable and keep using this
int pillUpdate = pillNumber - medsToTake;
Meds updatePlanDay = medsRepo.findById(medsId).get(); //trying to get the same as you did above
Should be
Meds updatePlanDay = medsRepo.getReferenceById(medsId);
int pillNumber = updatePlanDay.getPillNumber();
int pillUpdate = pillNumber - medsToTake;
//Meds updatePlanDay = medsRepo.findById(medsId).get(); no longer needed
Also on a different note you should look into the difference between findById and getReferenceById and what happens when there is no Meds with that medsId

JPA CriteriaBuilder with CONCAT_WS function throws NullPointerException

I have the following CriteriaQuery that I use to filter orders.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<OrderReducedDTO> cq = cb.createQuery(OrderReducedDTO.class);
Root<Order> root = cq.from(Order.class);
Join<Order, Customer> joinCustomer = root.join(Order_.customer);
Join<Order, Shipment> joinShipment = root.join(Order_.shipment);
Join<Shipment, Carrier> joinCarrier = joinShipment.join(Shipment_.carrier);
Join<Order, Payment> joinPayment = root.join(Order_.payment);
Join<Payment, PaymentMethod> joinPaymentMethod = joinPayment.join(Payment_.paymentMethod);
Join<Shipment, Country> joinCountry = joinShipment.join(Shipment_.country);
cq.select(cb.construct(
OrderReducedDTO.class,
root.get(Order_.id),
root.get(Order_.incrementId),
root.get(Order_.state),
root.get(Order_.couponCode),
root.get(Order_.totalDiscount),
root.get(Order_.total),
root.get(Order_.originChannel),
root.get(Order_.branchOffice),
joinCarrier.get(Carrier_.carrierCode),
cb.function("CONCAT_WS", String.class,
cb.literal(","),
joinShipment.get(Shipment_.streetName),
joinShipment.get(Shipment_.streetNumber),
joinShipment.get(Shipment_.city),
joinCountry.get(Country_.name),
joinShipment.get(Shipment_.zipCode)
),
joinPaymentMethod.get(PaymentMethod_.code),
joinPayment.get(Payment_.paymentDate),
root.get(Order_.createdAt),
root.get(Order_.updatedAt),
root.get(Order_.externalId),
joinCustomer.get(Customer_.fullName)
));
... filters and predicates...
The part that's giving me trouble and causing a NPE to be thrown is this
cb.function("CONCAT_WS", String.class,
cb.literal(","),
joinShipment.get(Shipment_.streetName),
joinShipment.get(Shipment_.streetNumber),
joinShipment.get(Shipment_.city),
joinCountry.get(Country_.name),
joinShipment.get(Shipment_.zipCode)
)
More, specifically, when I use the CONCAT_WS function. If I use CONCAT, it works.
This is the stacktrace I get:
java.lang.NullPointerException: null
at org.hibernate.hql.internal.NameGenerator.generateColumnNames(NameGenerator.java:27)
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.generateColumnNames(SessionFactoryHelper.java:434)
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeColumnNames(SelectClause.java:270)
at org.hibernate.hql.internal.ast.tree.SelectClause.finishInitialization(SelectClause.java:260)
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:255)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1026)
...
This is my OrderReducedDTO
#Getter
public class OrderReducedDTO {
#JsonProperty("order_id")
private Integer orderId;
#JsonProperty("increment_id")
private String incrementId;
private OrderStates state;
#JsonProperty("coupon_code")
private String couponCode;
#JsonProperty("total_discount")
private BigDecimal totalDiscount;
private BigDecimal total;
#JsonProperty("origin_channel")
private String originChannel;
#JsonProperty("branch_office")
private String branchOffice;
#JsonProperty("shipping_method")
private String shippingMethod;
#JsonProperty("shipping_address")
private String shippingAddress;
#JsonProperty("payment_method")
private String paymentMethod;
#JsonProperty("payment_date")
private Timestamp paymentDate;
#JsonProperty("created_at")
private Timestamp createdAt;
#JsonProperty("updated_at")
private Timestamp updatedAt;
#JsonProperty("external_id")
private String externalId;
#JsonProperty("customer_full_name")
private String customerFullName;
#Setter
private List<OrderProductReducedDTO> products;
public OrderReducedDTO(Integer orderId,
String incrementId,
OrderStates state,
String couponCode,
BigDecimal totalDiscount,
BigDecimal total,
String originChannel,
String branchOffice,
String shippingMethod,
String shippingAddress,
String paymentMethod,
Object paymentDate,
Object createdAt,
Object updatedAt,
String externalId,
String customerFullName) {
this.orderId = orderId;
this.incrementId = incrementId;
this.state = state;
this.couponCode = couponCode;
this.totalDiscount = totalDiscount;
this.total = total;
this.originChannel = originChannel;
this.branchOffice = branchOffice;
this.shippingMethod = shippingMethod;
this.shippingAddress = shippingAddress;
this.paymentMethod = paymentMethod;
this.paymentDate = (Timestamp) paymentDate;
this.createdAt = (Timestamp) createdAt; //https://hibernate.atlassian.net/browse/HHH-4179
this.updatedAt = (Timestamp) updatedAt;
this.externalId = externalId;
this.customerFullName = customerFullName;
}
}
What I mainly want to know is if I'm using the function method correctly. I assume I am because CONCAT works.
After hours of debugging within Hibernate, I finally arrived at the root of the problem:
org/hibernate/hql/internal/ast/tree/ConstructorNode.java
private Type[] resolveConstructorArgumentTypes() throws SemanticException {
SelectExpression[] argumentExpressions = this.collectSelectExpressions();
if (argumentExpressions == null) {
return new Type[0];
} else {
Type[] types = new Type[argumentExpressions.length];
for(int x = 0; x < argumentExpressions.length; ++x) {
types[x] = argumentExpressions[x].getDataType();
}
return types;
}
}
argumentExpressions[x].getDataType() was returning null.
I googled and found out that this could be caused by Hibernate not knowing the actual return type of the given SQL function (apparently it only knows the most common ones).
I then followed this answer and implemented a custom MetadataBuilderContributor like so:
public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {
#Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction(
"concat_ws",
new StandardSQLFunction("concat_ws", StandardBasicTypes.STRING)
);
}
}
And on my application.properties I added:
spring.jpa.properties.hibernate.metadata_builder_contributor=ar.com.glamit.glamitoms.config.hibernate.SqlFunctionsMetadataBuilderContributor
After relaunching the app, argumentExpressions[x].getDataType() now returns a StringType and the NullPointerException is gone.

How to Convert MongoDB Query to Spring Data Query

I'm trying to convert the following Mongo query for use with Spring data.
db.product.aggregate([
{$unwind: '$barcodes'},
{$project: {
_id: 0,
productId: '$_id',
productTitle: '$title',
productVariation: '$variation',
barcode: '$barcodes'
}}])
This is what I've been trying so far. It returns the aggregation, but with null values:
UnwindOperation unwindOperation = Aggregation.unwind("barcodes");
ProjectionOperation projectStage = Aggregation.project().and("productId").as("_id").and("productTitle")
.as("title")
.and("productVariation").as("variation")
.and("barcodeTitle").as("barcodes.title")
.and("barcodeValue").as("barcodes.value")
.and("barcodeType").as("barcodes.type")
.and("codeStandard").as("barcodes.codeStandard")
.and("quantity").as("barcodes.quantity")
.and("status").as("barcodes.status");
SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "title"));
Aggregation agg = Aggregation.newAggregation(unwindOperation, projectStage, sortOperation);
AggregationResults<BarcodeAggregateList> results = mongoTemplate.aggregate(agg, "product", BarcodeAggregateList.class);
What it is returning:
The class I am mapping to (has getters/setters):
public class BarcodeAggregateList {
private String productId;
private String productTitle;
private String productVariation;
private String barcodeTitle;
private String barcodeValue;
private String barcodeType;
private String codeStandard;
private int quantity;
private String status;
}
Product class that the data is coming from:
public class Product implements Serializable {
private static final long serialVersionUID = -998149317494604215L;
private String id;
private String title;
private String description;
private String SKU;
private double cost;
private double retailPrice;
private String status;
private LocalDate launchDate;
private LocalDate discontinueDate;
private String discontinueReason;
private String salesChannel;
private List<Barcode> barcodes;
private ProductVariation variation;
private List<Supplier> supplier;
private Product parentProduct;
private boolean updateChildren;
private Label label;
private int secondaryStockLevel;
private int primaryStockLevel;
private Date createdDate;
private Date modifiedDate;
private List<Dimension> dimensions;
private boolean isDeleted = false;
}
Barcode class
public class Barcode {
private String type;
private String title;
private String value;
private String status;
private String codeStandard;
private int quantity;
}
I appreciate any help with this or resources to help me better understand how to perform these types of conversions.
For anyone trying to solve similar issues, I've found the following resources somewhat helpful:
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query
https://xpadro.com/2016/04/data-aggregation-with-spring-data-mongodb-and-spring-boot.html
https://www.tutorialspoint.com/get-fields-from-multiple-sub-documents-that-match-a-condition-in-mongodb
BarcodeAggregateList class fields are null because there is a minor issue in ProjectionOperation's and() and as() methods. The correct syntax is
Aggregation.project().and(SOURCE_FIELD).as(TARGET_FIELD)
You have written and("productId").as("_id") , which is wrong
You need to write this as and("_id").as("productId") , because source field is _id
complete code:
UnwindOperation unwindOperation = Aggregation.unwind("barcodes");
ProjectionOperation projectStage = Aggregation.project()
.and("_id").as("productId")
.and("title").as("productTitle")
.and("variation").as("productVariation")
.and("barcodes.title").as("barcodeTitle")
.and("barcodes.value").as("barcodeValue")
.and("barcodes.type").as("barcodeType")
.and("barcodes.codeStandard").as("codeStandard")
.and("barcodes.quantity").as("quantity")
.and("barcodes.status").as("status");
SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "productTitle"));
Aggregation agg = Aggregation.newAggregation(unwindOperation, projectStage, sortOperation);
AggregationResults<BarcodeAggregateList> results = mongoTemplate.aggregate(agg, "product", BarcodeAggregateList.class);

JOOQ + JPA entities

I'm new with JOOQ library and have one thing interesting me so much. I've implemented CRUD service on JOOQ at first and after that I've tried to avoid some duplicate code. For reach that goal I've added JPA repository and also added#Entity annotation to my generated by JOOQ class. And now I still want to use JOOQ for some cases (querying List using filter and sorting and pagination). But something went wrong and now after JOOQ makes a select request I can see nulls in my class's attributes.
I'm getting right count of entities by filter, but class's properties are null after mapping. Is that mapping wrong or I just could't use JOOQ and JPA together for this case?
My abstact class for all entities (as I said, for avoid duplicating code I've refactored some code and now use generics):
#MappedSuperclass
public abstract class AbstractServiceEntity {
private Integer id;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
My JPA class (generated by JOOQ):
/**
* This class is generated by jOOQ.
*/
#SuppressWarnings({ "all", "unchecked", "rawtypes" })
#Entity
#Table(schema = "ref", name = "account")
public class Account extends AbstractServiceEntity implements Serializable {
private static final long serialVersionUID = -162537472;
private Integer id;
private Integer transitId;
private Integer partnerId;
private String currencyCode;
private String descr;
private Long inCredit;
private Long balanceLimit;
private Long outCredit;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer transitPartnerId;
public Account() {}
public Account(Account value) {
this.id = value.id;
this.transitId = value.transitId;
this.partnerId = value.partnerId;
this.currencyCode = value.currencyCode;
this.descr = value.descr;
this.inCredit = value.inCredit;
this.balanceLimit = value.balanceLimit;
this.outCredit = value.outCredit;
this.createdAt = value.createdAt;
this.updatedAt = value.updatedAt;
this.transitPartnerId = value.transitPartnerId;
}
public Account(
Integer id,
Integer transitId,
Integer partnerId,
String currencyCode,
String descr,
Long inCredit,
Long balanceLimit,
Long outCredit,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Integer transitPartnerId
) {
this.id = id;
this.transitId = transitId;
this.partnerId = partnerId;
this.currencyCode = currencyCode;
this.descr = descr;
this.inCredit = inCredit;
this.balanceLimit = balanceLimit;
this.outCredit = outCredit;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.transitPartnerId = transitPartnerId;
}
And my method extracting entities from DB:
#Repository
#RequiredArgsConstructor
public class JooqAccountRepository {
private final DSLContext jooq;
public List<Account> findAll(Condition filterCondition, SortField[] sortFields, Integer partnerId, Integer limit, Integer offset) {
return jooq.selectFrom(ACCOUNT)
.where(ACCOUNT.PARTNER_ID.equal(partnerId))
.and(filterCondition)
.orderBy(sortFields)
.limit(limit)
.offset(offset)
.fetchInto(Account.class);
}
public Integer findAccountsCount(Integer partnerId) {
return jooq.selectCount().from(ACCOUNT)
.where(ACCOUNT.PARTNER_ID.equal(partnerId))
.fetchOne(0, Integer.class);
}
}
As a result of my searches - I've made a mistake with annotations in Account class. If you want use these frameworks together, you should use #Column on entity's properties or setting your jooq's codegen plugin in different way)
This resource was usefull for me

Hibernate merge with flush not updating data

I've finding a solution but nothing works for me, here's the code:
Update function:
#Autowired
private SessionFactory sessionFactory;
...
public void updatePositionProfile(PositionProfile positionProfile) {
Session session = sessionFactory.getCurrentSession();
session.merge(positionProfile);
session.flush();
}
Entity (getters and setter ommited):
#Entity
#Table(name = "position_profile")
public class PositionProfile implements Serializable {
#Embeddable
public static class PositionProfile_PK implements Serializable {
private static final long serialVersionUID = 1L;
#NotNull
#Column(name="id_position")
Integer id_position;
#NotNull
#Column(name="profile")
String profile;
#NotNull
#Column(name="line")
String line;
PositionProfile_PK(){
this.id_position = 0;
this.profile = new String();
this.line = "";
}
}
#Id
PositionProfile_PK positionProfilePK;
#NotNull
#Column(name="MAX_SPEED")
private Integer max_speed;
#NotNull
#Column(name="WARNING_SPEED")
private Integer warning_speed;
#NotNull
#Column(name="EMERGENCY_SPEED")
private Integer emergency_speed;
#NotNull
#Column(name="DISABLED")
private String disabled;
PositionProfile(){
super();
this.positionProfilePK = new PositionProfile_PK();
this.max_speed = 0;
this.warning_speed = 0;
this.emergency_speed = 0;
this.disabled = " ";
}
}
Controller (summarized for brevity):
PositionProfile positionProfileToUpdate = positionProfile.getPositionProfileByIdPositionAndProfile(pk, profile);
positionProfileToUpdate.setMax_speed(ms);
positionProfile.updatePositionProfile(positionProfileToUpdate);
I've tryed with update() function and saveOrUpdate() but it doesn't work, I don't know what's happening. Session is never closed so the entity is attached. I've checked that values are changed correctly in the object I passed to updatePositionProfile() function, but when merge() it simply does nothing.
Thanks!
if you make flush, the PositionProfile is only for the same session visible.
You must check, if the sessionFactory.getCurrentSession(); working correctly

Categories

Resources