Cassandra Spring No Converter Found Capable of Converting from Type Instant to type Long - spring

Am getting a No converter found capable of converting from type Instant to type Long while using Spring and Cassandra.
I have 2 Instant fields writtenDate (in the object primary key) and hotelAddedDate in the object. However, I do not have any Long fields; thus, I am a little confused.
Does anyone have an idea how to fix this error?
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.Instant] to type [java.lang.Long]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.convertIfNecessary(ConvertingPropertyAccessor.java:120)
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.getProperty(ConvertingPropertyAccessor.java:91)
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.getWriteValue(MappingCassandraConverter.java:771)
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.writeInternal(MappingCassandraConverter.java:518)
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.writeInternal(MappingCassandraConverter.java:513)
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.write(MappingCassandraConverter.java:484)
at org.springframework.data.cassandra.core.StatementFactory.insert(StatementFactory.java:308)
at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:674)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:664)
at org.springframework.data.cassandra.repository.support.SimpleCassandraRepository.save(SimpleCassandraRepository.java:96)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:639)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy199.save(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy199.save(Unknown Source)
at com.saathratri.tajvote.service.impl.CustomerReviewsByHotelAndLastNameAndFirstNameServiceImpl.save(CustomerReviewsByHotelAndLastNameAndFirstNameServiceImpl.java:36)
at com.saathratri.tajvote.service.impl.CustomerReviewsByHotelAndLastNameAndFirstNameServiceImpl$$FastClassBySpringCGLIB$$da12f07b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
at com.saathratri.tajvote.aop.logging.LoggingAspect.logAround(LoggingAspect.java:105)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
at com.saathratri.tajvote.service.impl.CustomerReviewsByHotelAndLastNameAndFirstNameServiceImpl$$EnhancerBySpringCGLIB$$eda9c521.save(<generated>)
at com.saathratri.tajvote.web.rest.CustomerReviewsByHotelAndLastNameAndFirstNameResource.createCustomerReviewsByHotelAndLastNameAndFirstName(CustomerReviewsByHotelAndLastNameAndFirstNameResource.java:77)
at com.saathratri.tajvote.web.rest.CustomerReviewsByHotelAndLastNameAndFirstNameResource$$FastClassBySpringCGLIB$$d91d43fd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
...
Here is my CQL for creating the table in Cassandra:
create table customer_reviews_by_hotel_and_last_name_and_first_name (
hotel_id uuid,
last_name text,
first_name text,
written_date timestamp,
review_id timeuuid,
customer_id uuid,
customer_email_addresses set<text>,
customer_phone_numbers set<text>,
added_source text,
main_star_rating tinyint,
comments text,
enjoyed_stay boolean,
would_recommend boolean,
hotel_added_date timestamp,
hotel_name text,
hotel_email text,
hotel_phone_number text,
primary key((hotel_id), last_name, first_name, written_date, review_id))
with clustering order by (last_name desc, first_name desc, written_date desc, review_id asc);
This is my primary key class:
package com.saathratri.tajvote.domain;
import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyClass;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
#PrimaryKeyClass
public class CustomerReviewsByHotelAndLastNameAndFirstNameId
implements java.io.Serializable {
#PrimaryKeyColumn(
name = "hotel_id",
ordinal = 0,
type = PrimaryKeyType.PARTITIONED
)
#CassandraType(type = CassandraType.Name.UUID)
private UUID hotelId;
#PrimaryKeyColumn(
name = "last_name",
ordinal = 1,
type = PrimaryKeyType.CLUSTERED
)
#CassandraType(type = CassandraType.Name.TEXT)
private String lastName;
#PrimaryKeyColumn(
name = "first_name",
ordinal = 2,
type = PrimaryKeyType.CLUSTERED
)
#CassandraType(type = CassandraType.Name.TEXT)
private String firstName;
#PrimaryKeyColumn(
name = "written_date",
ordinal = 3,
type = PrimaryKeyType.CLUSTERED
)
#CassandraType(type = CassandraType.Name.BIGINT)
private Instant writtenDate;
#PrimaryKeyColumn(
name = "review_id",
ordinal = 4,
type = PrimaryKeyType.CLUSTERED
)
#CassandraType(type = CassandraType.Name.UUID)
private UUID reviewId;
public CustomerReviewsByHotelAndLastNameAndFirstNameId() {}
public CustomerReviewsByHotelAndLastNameAndFirstNameId(
UUID hotelId,
String lastName,
String firstName,
Instant writtenDate,
UUID reviewId
) {
this.hotelId = hotelId;
this.lastName = lastName;
this.firstName = firstName;
this.writtenDate = writtenDate;
this.reviewId = reviewId;
}
public UUID getHotelId() {
return this.hotelId;
}
public void setHotelId(UUID hotelId) {
this.hotelId = hotelId;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Instant getWrittenDate() {
return this.writtenDate;
}
public void setWrittenDate(Instant writtenDate) {
this.writtenDate = writtenDate;
}
public UUID getReviewId() {
return this.reviewId;
}
public void setReviewId(UUID reviewId) {
this.reviewId = reviewId;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CustomerReviewsByHotelAndLastNameAndFirstNameId)) {
return false;
}
CustomerReviewsByHotelAndLastNameAndFirstNameId customerReviewsByHotelAndLastNameAndFirstNameId = (CustomerReviewsByHotelAndLastNameAndFirstNameId) o;
return (
Objects.equals(
hotelId,
customerReviewsByHotelAndLastNameAndFirstNameId.hotelId
) &&
Objects.equals(
lastName,
customerReviewsByHotelAndLastNameAndFirstNameId.lastName
) &&
Objects.equals(
firstName,
customerReviewsByHotelAndLastNameAndFirstNameId.firstName
) &&
Objects.equals(
writtenDate,
customerReviewsByHotelAndLastNameAndFirstNameId.writtenDate
) &&
Objects.equals(
reviewId,
customerReviewsByHotelAndLastNameAndFirstNameId.reviewId
)
);
}
#Override
public int hashCode() {
return Objects.hash(hotelId, lastName, firstName, writtenDate, reviewId);
}
// prettier-ignore
#Override
public String toString() {
return "CustomerReviewsByHotelAndLastNameAndFirstNameId{" +
", hotelId='" + getHotelId() + "'" +
", lastName='" + getLastName() + "'" +
", firstName='" + getFirstName() + "'" +
", writtenDate='" + getWrittenDate() + "'" +
", reviewId='" + getReviewId() + "'" +
"}";
}
}
This my domain class:
package com.saathratri.tajvote.domain;
import java.io.Serializable;
import java.time.Instant;
import java.util.Set;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
/**
* A CustomerReviewsByHotelAndLastNameAndFirstName.
*/
#Table("customer_reviews_by_hotel_and_last_name_and_first_name")
public class CustomerReviewsByHotelAndLastNameAndFirstName
implements Serializable {
private static final long serialVersionUID = 1L;
#PrimaryKey
private CustomerReviewsByHotelAndLastNameAndFirstNameId id;
#Column("customer_id")
#CassandraType(type = CassandraType.Name.UUID)
private UUID customerId;
#Column("customer_email_addresses")
#CassandraType(
type = CassandraType.Name.SET,
typeArguments = CassandraType.Name.TEXT
)
private Set<String> customerEmailAddresses;
#Column("customer_phone_numbers")
#CassandraType(
type = CassandraType.Name.SET,
typeArguments = CassandraType.Name.TEXT
)
private Set<String> customerPhoneNumbers;
#Column("added_source")
#CassandraType(type = CassandraType.Name.TEXT)
private String addedSource;
#Column("main_star_rating")
#CassandraType(type = CassandraType.Name.TINYINT)
private Integer mainStarRating;
#Column("comments")
#CassandraType(type = CassandraType.Name.TEXT)
private String comments;
#Column("enjoyed_stay")
#CassandraType(type = CassandraType.Name.BOOLEAN)
private Boolean enjoyedStay;
#Column("would_recommend")
#CassandraType(type = CassandraType.Name.BOOLEAN)
private Boolean wouldRecommend;
#Column("hotel_added_date")
#CassandraType(type = CassandraType.Name.BIGINT)
private Instant hotelAddedDate;
#Column("hotel_name")
#CassandraType(type = CassandraType.Name.TEXT)
private String hotelName;
#Column("hotel_email")
#CassandraType(type = CassandraType.Name.TEXT)
private String hotelEmail;
#Column("hotel_phone_number")
#CassandraType(type = CassandraType.Name.TEXT)
private String hotelPhoneNumber;
public CustomerReviewsByHotelAndLastNameAndFirstNameId getId() {
return id;
}
public void setId(CustomerReviewsByHotelAndLastNameAndFirstNameId id) {
this.id = id;
}
public CustomerReviewsByHotelAndLastNameAndFirstName id(
CustomerReviewsByHotelAndLastNameAndFirstNameId id
) {
this.id = id;
return this;
}
// jhipster-needle-entity-add-field - JHipster will add fields here
public UUID getCustomerId() {
return this.customerId;
}
public CustomerReviewsByHotelAndLastNameAndFirstName customerId(
UUID customerId
) {
this.setCustomerId(customerId);
return this;
}
public void setCustomerId(UUID customerId) {
this.customerId = customerId;
}
public Set<String> getCustomerEmailAddresses() {
return this.customerEmailAddresses;
}
public CustomerReviewsByHotelAndLastNameAndFirstName customerEmailAddresses(
Set<String> customerEmailAddresses
) {
this.setCustomerEmailAddresses(customerEmailAddresses);
return this;
}
public void setCustomerEmailAddresses(Set<String> customerEmailAddresses) {
this.customerEmailAddresses = customerEmailAddresses;
}
public Set<String> getCustomerPhoneNumbers() {
return this.customerPhoneNumbers;
}
public CustomerReviewsByHotelAndLastNameAndFirstName customerPhoneNumbers(
Set<String> customerPhoneNumbers
) {
this.setCustomerPhoneNumbers(customerPhoneNumbers);
return this;
}
public void setCustomerPhoneNumbers(Set<String> customerPhoneNumbers) {
this.customerPhoneNumbers = customerPhoneNumbers;
}
public String getAddedSource() {
return this.addedSource;
}
public CustomerReviewsByHotelAndLastNameAndFirstName addedSource(
String addedSource
) {
this.setAddedSource(addedSource);
return this;
}
public void setAddedSource(String addedSource) {
this.addedSource = addedSource;
}
public Integer getMainStarRating() {
return this.mainStarRating;
}
public CustomerReviewsByHotelAndLastNameAndFirstName mainStarRating(
Integer mainStarRating
) {
this.setMainStarRating(mainStarRating);
return this;
}
public void setMainStarRating(Integer mainStarRating) {
this.mainStarRating = mainStarRating;
}
public String getComments() {
return this.comments;
}
public CustomerReviewsByHotelAndLastNameAndFirstName comments(
String comments
) {
this.setComments(comments);
return this;
}
public void setComments(String comments) {
this.comments = comments;
}
public Boolean getEnjoyedStay() {
return this.enjoyedStay;
}
public CustomerReviewsByHotelAndLastNameAndFirstName enjoyedStay(
Boolean enjoyedStay
) {
this.setEnjoyedStay(enjoyedStay);
return this;
}
public void setEnjoyedStay(Boolean enjoyedStay) {
this.enjoyedStay = enjoyedStay;
}
public Boolean getWouldRecommend() {
return this.wouldRecommend;
}
public CustomerReviewsByHotelAndLastNameAndFirstName wouldRecommend(
Boolean wouldRecommend
) {
this.setWouldRecommend(wouldRecommend);
return this;
}
public void setWouldRecommend(Boolean wouldRecommend) {
this.wouldRecommend = wouldRecommend;
}
public Instant getHotelAddedDate() {
return this.hotelAddedDate;
}
public CustomerReviewsByHotelAndLastNameAndFirstName hotelAddedDate(
Instant hotelAddedDate
) {
this.setHotelAddedDate(hotelAddedDate);
return this;
}
public void setHotelAddedDate(Instant hotelAddedDate) {
this.hotelAddedDate = hotelAddedDate;
}
public String getHotelName() {
return this.hotelName;
}
public CustomerReviewsByHotelAndLastNameAndFirstName hotelName(
String hotelName
) {
this.setHotelName(hotelName);
return this;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public String getHotelEmail() {
return this.hotelEmail;
}
public CustomerReviewsByHotelAndLastNameAndFirstName hotelEmail(
String hotelEmail
) {
this.setHotelEmail(hotelEmail);
return this;
}
public void setHotelEmail(String hotelEmail) {
this.hotelEmail = hotelEmail;
}
public String getHotelPhoneNumber() {
return this.hotelPhoneNumber;
}
public CustomerReviewsByHotelAndLastNameAndFirstName hotelPhoneNumber(
String hotelPhoneNumber
) {
this.setHotelPhoneNumber(hotelPhoneNumber);
return this;
}
public void setHotelPhoneNumber(String hotelPhoneNumber) {
this.hotelPhoneNumber = hotelPhoneNumber;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CustomerReviewsByHotelAndLastNameAndFirstName)) {
return false;
}
return (
id != null &&
id.equals(((CustomerReviewsByHotelAndLastNameAndFirstName) o).id)
);
}
#Override
public int hashCode() {
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
}
// prettier-ignore
#Override
public String toString() {
return "CustomerReviewsByHotelAndLastNameAndFirstName{" +
"id=" + getId() +
", customerId='" + getCustomerId() + "'" +
", customerEmailAddresses='" + getCustomerEmailAddresses() + "'" +
", customerPhoneNumbers='" + getCustomerPhoneNumbers() + "'" +
", addedSource='" + getAddedSource() + "'" +
", mainStarRating=" + getMainStarRating() +
", comments='" + getComments() + "'" +
", enjoyedStay='" + getEnjoyedStay() + "'" +
", wouldRecommend='" + getWouldRecommend() + "'" +
", hotelAddedDate='" + getHotelAddedDate() + "'" +
", hotelName='" + getHotelName() + "'" +
", hotelEmail='" + getHotelEmail() + "'" +
", hotelPhoneNumber='" + getHotelPhoneNumber() + "'" +
"}";
}
}

Had to change CassandraType.Name.BIGINT to CassandraType.Name.TIMESTAMP:
In CustomerReviewsByHotelAndLastNameAndFirstName.java:
...
#Column("hotel_added_date")
#CassandraType(type = CassandraType.Name.TIMESTAMP)
private Instant hotelAddedDate;
...
In CustomerReviewsByHotelAndLastNameAndFirstNameId.java:
...
#PrimaryKeyColumn(
name = "written_date",
ordinal = 3,
type = PrimaryKeyType.CLUSTERED
)
#CassandraType(type = CassandraType.Name.TIMESTAMP)
private Instant writtenDate;
...

Related

springdatarepository ElasticSearch works on save but fails on update with java.lang.StackOverflowError: null

I am getting java.lang.StackOverflowError: null when updating my data through springdataelasticsearch. The application was generated using Jhipster 7.0.1. Spring Boot version is 2.4.4.. It works fine when creating a new object but an update results in error. Here is the relevant portion of CountryAdminUnitTypeService.java.
* Save a countryAdminUnitType.
*
* #param countryAdminUnitType the entity to save.
* #return the persisted entity.
*/
public CountryAdminUnitType save(CountryAdminUnitType countryAdminUnitType) {
log.debug("Request to save CountryAdminUnitType : {}", countryAdminUnitType);
CountryAdminUnitType result = countryAdminUnitTypeRepository.save(countryAdminUnitType);
countryAdminUnitTypeSearchRepository.save(result);
return result;
}
When creating, here's how the object looks on call to countryAdminUntiTypeSearchRepository.save(result);.
result = {CountryAdminUnitType#23268} "CountryAdminUnitType{id=200102, name='Province'}"
id = {Long#23292} 200102
name = "Province"
parent = null
country = {Country#23275} "Country{id=183778, name='Turkey', isoCode='TR'}"
id = {Long#23281} 183778
name = "Turkey"
isoCode = "TR"
countryAdminUnitTypes = null
And here's how it looks on updating.
result = {CountryAdminUnitType#24746} "CountryAdminUnitType{id=200102, name='Province'}"
id = {Long#24752} 200102
name = "Province"
parent = null
country = {Country#24754} "Country{id=183778, name='Turkey', isoCode='TR'}"
id = {Long#24756} 183778
name = "Turkey"
isoCode = "TR"
countryAdminUnitTypes = {PersistentSet#24761} size = 1
0 = {CountryAdminUnitType#24746} "CountryAdminUnitType{id=200102, name='Province'}"
id = {Long#24752} 200102
name = "Province"
parent = null
country = {Country#24754} "Country{id=183778, name='Turkey', isoCode='TR'}"
The only difference I see is that countryAdminUnitTypes is not null in second case. However this should be taken care of by JsonIgnoreProperties given in Country.java below.
Here's the beginning of a very long log file.
ERROR 91444 --- [ XNIO-5 task-1] c.s.c.s.CountryAdminUnitTypeService : Exception in save() with cause = 'NULL' and exception = 'null'
java.lang.StackOverflowError: null
at org.springframework.data.util.Streamable.stream(Streamable.java:87)
at org.springframework.data.util.Streamable.lambda$map$1(Streamable.java:101)
at org.springframework.data.util.LazyStreamable.stream(LazyStreamable.java:55)
at org.springframework.data.util.LazyStreamable.iterator(LazyStreamable.java:46)
at java.base/java.lang.Iterable.forEach(Iterable.java:74)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeCollectionValue(MappingElasticsearchConverter.java:710)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:620)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperty(MappingElasticsearchConverter.java:601)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperties(MappingElasticsearchConverter.java:553)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:511)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:636)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:627)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperty(MappingElasticsearchConverter.java:601)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperties(MappingElasticsearchConverter.java:553)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:511)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:636)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:627)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.lambda$writeCollectionValue$7(MappingElasticsearchConverter.java:709)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$0(StreamSpliterators.java:294)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.base/java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at java.base/java.lang.Iterable.forEach(Iterable.java:74)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeCollectionValue(MappingElasticsearchConverter.java:710)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:620)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperty(MappingElasticsearchConverter.java:601)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperties(MappingElasticsearchConverter.java:553)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:511)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:636)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:627)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperty(MappingElasticsearchConverter.java:601)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeProperties(MappingElasticsearchConverter.java:553)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:511)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.writeEntity(MappingElasticsearchConverter.java:636)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.getWriteComplexValue(MappingElasticsearchConverter.java:627)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.lambda$writeCollectionValue$7(MappingElasticsearchConverter.java:709)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$0(StreamSpliterators.java:294)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.base/java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at java.base/java.lang.Iterable.forEach(Iterable.java:74)
Here are the model classes.
CountryAdminUnitType.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* A CountryAdminUnitType.
*/
#Entity
#Table(name = "country_admin_unit_type")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
#org.springframework.data.elasticsearch.annotations.Document(indexName = "countryadminunittype")
public class CountryAdminUnitType implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
#Column(name = "name")
private String name;
#ManyToOne
#JsonIgnoreProperties(value = { "parent", "country" }, allowSetters = true)
private CountryAdminUnitType parent;
#ManyToOne
#JsonIgnoreProperties(
value = { "defaultResidenceMeasurementUnit", "countryAdminUnitTypes", "preferences", "personNationalities" },
allowSetters = true
)
private Country country;
// jhipster-needle-entity-add-field - JHipster will add fields here
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public CountryAdminUnitType id(Long id) {
this.id = id;
return this;
}
public String getName() {
return this.name;
}
public CountryAdminUnitType name(String name) {
this.name = name;
return this;
}
public void setName(String name) {
this.name = name;
}
public CountryAdminUnitType getParent() {
return this.parent;
}
public CountryAdminUnitType parent(CountryAdminUnitType countryAdminUnitType) {
this.setParent(countryAdminUnitType);
return this;
}
public void setParent(CountryAdminUnitType countryAdminUnitType) {
this.parent = countryAdminUnitType;
}
public Country getCountry() {
return this.country;
}
public CountryAdminUnitType country(Country country) {
this.setCountry(country);
return this;
}
public void setCountry(Country country) {
this.country = country;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CountryAdminUnitType)) {
return false;
}
return id != null && id.equals(((CountryAdminUnitType) o).id);
}
#Override
public int hashCode() {
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
}
// prettier-ignore
#Override
public String toString() {
return "CountryAdminUnitType{" +
"id=" + getId() +
", name='" + getName() + "'" +
"}";
}
}
Country.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* A Country.
*/
#Entity
#Table(name = "country")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
#org.springframework.data.elasticsearch.annotations.Document(indexName = "country")
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
#Column(name = "name")
private String name;
#Column(name = "iso_code")
private String isoCode;
#OneToMany(mappedBy = "country")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
#JsonIgnoreProperties(value = { "parent", "country" })
private Set<CountryAdminUnitType> countryAdminUnitTypes = new HashSet<>();
// jhipster-needle-entity-add-field - JHipster will add fields here
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Country id(Long id) {
this.id = id;
return this;
}
public String getName() {
return this.name;
}
public Country name(String name) {
this.name = name;
return this;
}
public void setName(String name) {
this.name = name;
}
public String getIsoCode() {
return this.isoCode;
}
public Country isoCode(String isoCode) {
this.isoCode = isoCode;
return this;
}
public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
public Set<CountryAdminUnitType> getCountryAdminUnitTypes() {
return this.countryAdminUnitTypes;
}
public Country countryAdminUnitTypes(Set<CountryAdminUnitType> countryAdminUnitTypes) {
this.setCountryAdminUnitTypes(countryAdminUnitTypes);
return this;
}
public Country addCountryAdminUnitType(CountryAdminUnitType countryAdminUnitType) {
this.countryAdminUnitTypes.add(countryAdminUnitType);
countryAdminUnitType.setCountry(this);
return this;
}
public Country removeCountryAdminUnitType(CountryAdminUnitType countryAdminUnitType) {
this.countryAdminUnitTypes.remove(countryAdminUnitType);
countryAdminUnitType.setCountry(null);
return this;
}
public void setCountryAdminUnitTypes(Set<CountryAdminUnitType> countryAdminUnitTypes) {
if (this.countryAdminUnitTypes != null) {
this.countryAdminUnitTypes.forEach(i -> i.setCountry(null));
}
if (countryAdminUnitTypes != null) {
countryAdminUnitTypes.forEach(i -> i.setCountry(this));
}
this.countryAdminUnitTypes = countryAdminUnitTypes;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Country)) {
return false;
}
return id != null && id.equals(((Country) o).id);
}
#Override
public int hashCode() {
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
}
// prettier-ignore
#Override
public String toString() {
return "Country{" +
"id=" + getId() +
", name='" + getName() + "'" +
", isoCode='" + getIsoCode() + "'" +
"}";
}
}
This is because of how JHipster sets up save. It was only happening on updating objects with bi-directional relationships.
* Save a countryAdminUnitType.
*
* #param countryAdminUnitType the entity to save.
* #return the persisted entity.
*/
public CountryAdminUnitType save(CountryAdminUnitType countryAdminUnitType) {
log.debug("Request to save CountryAdminUnitType : {}", countryAdminUnitType);
CountryAdminUnitType result = countryAdminUnitTypeRepository.save(countryAdminUnitType);
countryAdminUnitTypeSearchRepository.save(result);
return result;
}
The object to be updated is set correctly when being passed to jpa repository countryAdminUnitTypeRepository.save(countryAdminUnitType);. However, the returned result object has circular dependencies and is directly forwarded to elasticsearch repository countryAdminUnitTypeSearchRepository.save(result); for update. I resolved it by only using the id of the object.
Line
CountryAdminUnitType result = countryAdminUnitTypeRepository.save(countryAdminUnitType);
countryAdminUnitTypeSearchRepository.save(result);
has been changed to
CountryAdminUnitType result = countryAdminUnitTypeRepository.save(countryAdminUnitType);
countryAdminUnitType.setId(result.getId());
countryAdminUnitTypeSearchRepository.save(countryAdminUnitType);
return result;
Thank you for the discussion. This did not work for me on entity , using Jhipster 7.8.1.
FYI, I just commented out the SearchRepository.save(result); as a quick fix to unblock development.
Issue: https://github.com/jhipster/generator-jhipster/issues/16136#issuecomment-1012404392

500 Internal Server Error; when using POST method in springBoot rest api

I used Spring Boot, POST method for create a new score for my player.
In POST method, I check if the player and game exists then create new score and also add score and its related date into the history of my score's class.
Each score has history which contains score and its date. the history has type list of History's class
The History Class:
package thesisMongoProject;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "history")
public class History {
#Id
private String score;
private Date date;
public History(String score, Date date) {
super();
this.score = score;
this.date = date;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
#Override
public String toString() {
return "History [score=" + score + ", date=" + date + "]";
}
}
The Score Class:
package thesisMongoProject;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonView;
#Document(collection = "score")
public class Score {
#Id
#NotBlank
#JsonView(Views.class)
private String score;
#NotBlank
#JsonView(Views.class)
private String player;
#NotBlank
#JsonView(Views.class)
private String code;
#JsonView(Views.class)
private Date date;
private List<History> history;
public Score(#NotBlank String score, String player, String code, List<History> history, Date date) {
super();
this.score = score;
this.player = player;
this.code = code;
this.history = history;
this.date = date;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
#Override
public String toString() {
return "Score [score=" + score + ", player=" + player + ", code=" + code + ", history=" + history + ", date="
+ date + "]";
}
}
And the POST method:
#RestController
#RequestMapping("/score")
public class ScoreController {
#Autowired
private ScoreRepository srepo;
#Autowired
private PlayerRepository prepo;
#Autowired
private GamesRepository grepo;
#Autowired
private HistoryRepository hrepo;
private List<History> history;
private History h = null;
//Create Score
#PostMapping
public ResponseEntity<?> createScore(#RequestBody #JsonView(Views.class) #Valid Score score) {
//check player exist
Player p = prepo.findByNickname(score.getPlayer());
//check game's cod exist
Games g = grepo.findByCode(score.getCode());
//check score exist
Score s = srepo.findByScore(score.getScore());
// = hrepo.findByScore(score.getScore());
if(s != null)
{
return ResponseEntity.status(409).body("Conflict!!");
}else if((p != null) && (g != null)) {
h.setScore(score.getScore());
h.setDate(score.getDate());
hrepo.save(h);
history.add(h);
//history.add(hrepo.findById(score.getScore()).get());
score.setHistory(history);
srepo.save(score);
return ResponseEntity.status(201).body("Created!");
}
else {
return ResponseEntity.status(400).body("Bad Request!");
}
}
In my POST method I tried to setScore and setDate for an object of History class, and then I saved them by hrepo which is history Repository and then I added this to the history variable of type List<History>, after that I setHistory of my score class with srepo, Score repository . But when I execute my program, in PostMan I have 500 Internal Server Error and in the console I have this Error:
java.lang.NullPointerException: null
at thesisMongoProject.controller.ScoreController.createScore(ScoreController.java:63) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
which is exactly the line that I setScore of object h, h.setScore(score.getScore());
I cannot understand what is my mistake.
Initialize both, you should not get NPE after that
private List<History> history=new ArrayList<>();
private History h = new History();
field h in this line
private History h = null;
may be local variable like below.
}else if((p != null) && (g != null)) {
History h = new History(); //add local variable here.
h.setScore(score.getScore());

Count number of Item using Hibernate/JPA or JdbcTemplate

I am new to Spring/Hibernate/JPA. I have an entity class MovieEntity and MovieVersionEntity. MovieEntity has few details about the movie (like genre of movie) but MovieVersionEntity has more details about it (name, director...). So I want to count the number of movies (MovieVersionEntity) associated to the MovieEntity for the given type.
MovieEntity:
#Entity(name="MovieEntity")
#Table(name="Movie")
public class MovieEntity {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="Id")
private long id;
#Column(name="IsDeleted")
private boolean isDeleted;
#Column(name="ModifiedDate")
#Temporal(TemporalType.TIMESTAMP)
private Date modifiedDate;
#OneToOne()
#JoinColumn(name="MovieTypeId")
private MovieTypeEntity movieTypeEntity;
#OneToMany(mappedBy="movieEntity",optional = false)
private List<MovieVersionEntity> movieVersionEntity;
#Transient
//#Formula("select count(*) from movie_version mv where mv.id=id")
private int childCount;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}
public MovieTypeEntity getMovieTypeEntity() {
return movieTypeEntity;
}
public void setMovieTypeEntity(MovieTypeEntity movieTypeEntity) {
this.movieTypeEntity = movieTypeEntity;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public MovieVersionEntity getMovieVersionEntity() {
return movieVersionEntity;
}
public void setMovieVersionEntity(MovieVersionEntity movieVersionEntity) {
this.movieVersionEntity = movieVersionEntity;
}
public int getChildCount() {
return childCount;
}
public void setChildCount(int childCount) {
this.childCount = childCount;
}
}
MovieVersionEntity
#Entity(name = "MovieVersionEntity")
#Table(name="MovieVersion")
//#EntityListeners(AuditingEntityListener.class)
public class MovieVersionEntity {
#Id()
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="MovieId")
private long movieId;
#NotBlank
#Column(name="MovieName")
private String movieName;
#NotBlank
#Column(name="DirectorName")
private String directorName;
#NotBlank
#Column(name="Description")
private String description;
#Column(name="StopDate")
#Temporal(TemporalType.TIMESTAMP)
private Date stopDate;
#Column(name="DoneWatching")
private boolean doneWatching;
#Column(name="WatchDate")
#Temporal(TemporalType.TIMESTAMP)
//#CreatedDate
private Date watchDate;
#Column(name="ModifiedDate")
#Temporal(TemporalType.TIMESTAMP)
//#LastModifiedDate
private Date modifiedDate;
#ManyToOne(optional = false)
#JoinColumn(name="Id")
private MovieEntity movieEntity;
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getDirectorName() {
return directorName;
}
public void setDirectorName(String directorName) {
this.directorName = directorName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStopDate() {
return stopDate;
}
public void setStopDate(Date stopDate) {
this.stopDate = stopDate;
}
public boolean isDoneWatching() {
return doneWatching;
}
public void setDoneWatching(boolean doneWatching) {
this.doneWatching = doneWatching;
}
public Date getWatchDate() {
return watchDate;
}
public void setWatchDate(Date watchDate) {
this.watchDate = watchDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public long getMovieId() {
return movieId;
}
public void setMovieId(long movieId) {
this.movieId = movieId;
}
public MovieEntity getMovieEntity() {
return movieEntity;
}
public void setMovieEntity(MovieEntity movieEntity) {
this.movieEntity = movieEntity;
}
}
I have written a query but I am getting sql error for it
#Query(value = "select m.*, ct.ChildCount" +
"from (" +
"select mv.id, count(movie_id) as ChildCount " +
"from movie_version mv " +
"group by mv.id" +
") as ct join movie m " +
"on ct.id = m.id;",nativeQuery = true)
List<MovieEntity> getMoviesWithCount();
Error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select mv.id, count(movie_id) as ChildCount from movie_version mv group by mv.id' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_60]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_60]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar:5.1.44]
at
Also, I am not sure if this is a right way to do it. Is there any other way I can save the count in the Transient variable. I tried using #Formuala too, but that does not give me 0 count.
Formula:
#Formula("select count(*) from movie_version mv where mv.id=id")
This is the first time I am dealing with Transient variable and I am not sure how it maps to the entity if its not persisted in the db.
However, #Formula worked for me. #Transient and #Formula cannot go together. #Formula is read only so I do not have to worry about the data being persisted.
http://outbottle.com/hibernate-populating-an-unmapped-entity-field-with-count-using-formula/

DAO instance not working in service class - NullPointerException

In my spring boot project I created a Repository interface (which extends CRUDRepository) and an Entity class of the Table in my DB.
This is my Repo:
#Repository
public interface AuthPaymentDao extends CrudRepository<TFraudCard,String> {
#Query("SELECT t FROM TFraudCard t where t.tokenNumber = (?1)")
TFraudCard findByTokenNumber(String tokenNumber);
}
This is my Entity Class (TOKEN_NUMBER is the primary Key in the TFRAUDCARD TABLE):
#Entity
#Table(name = "TFRAUDCARD")
public class TFraudCard {
#Id
#Column(name="TOKEN_NUMBER")
private String tokenNumber;
#Column(name="TRANSACTIONNUMBER")
private int transactionNumber;
#Column(name="CARDNUMBER")
private int cardNumber;
#Column(name="DATEADDED", insertable = false, updatable = false, nullable = false)
private Timestamp dateAdded;
#Column(name="CALLINGENTITY", nullable = false)
private String callingEntity;
#Column(name="ACCOUNTID")
private String accountId;
#Column(name="ROUTINGNUMBER")
private String routingNumber;
#Column(name="BANKACCOUNTNUMBER")
private String bankAccountNumber;
#Column(name="COMMENTS")
private String comments;
#Column(name="USERID")
private String userId;
#Column(name="REMOVEDATE")
private Timestamp removeDate;
public String getTokenNumber() {
return tokenNumber;
}
public void setTokenNumber(String tokenNumber) {
this.tokenNumber = tokenNumber;
}
public int getTransactionNumber() {
return transactionNumber;
}
public void setTransactionNumber(int transactionNumber) {
this.transactionNumber = transactionNumber;
}
public int getCardNumber() {
return cardNumber;
}
public void setCardNumber(int cardNumber) {
this.cardNumber = cardNumber;
}
public Timestamp getDateAdded() {
return dateAdded;
}
public void setDateAdded(Timestamp dateAdded) {
this.dateAdded = dateAdded;
}
public String getCallingEntity() {
return callingEntity;
}
public void setCallingEntity(String callingEntity) {
this.callingEntity = callingEntity;
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public String getRoutingNumber() {
return routingNumber;
}
public void setRoutingNumber(String routingNumber) {
this.routingNumber = routingNumber;
}
public String getBankAccountNumber() {
return bankAccountNumber;
}
public void setBankAccountNumber(String bankAccountNumber) {
this.bankAccountNumber = bankAccountNumber;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Timestamp getRemoveDate() {
return removeDate;
}
public void setRemoveDate(Timestamp removeDate) {
this.removeDate = removeDate;
}
public TFraudCard() {
super();
}
public TFraudCard(String tokenNumber, int transactionNumber, int cardNumber, Timestamp dateAdded,
String callingEntity, String accountId, String routingNumber, String bankAccountNumber, String comments,
String userId, Timestamp removeDate) {
super();
this.tokenNumber = tokenNumber;
this.transactionNumber = transactionNumber;
this.cardNumber = cardNumber;
this.dateAdded = dateAdded;
this.callingEntity = callingEntity;
this.accountId = accountId;
this.routingNumber = routingNumber;
this.bankAccountNumber = bankAccountNumber;
this.comments = comments;
this.userId = userId;
this.removeDate = removeDate;
}
#Override
public String toString() {
return "TFraudCard [tokenNumber=" + tokenNumber + ", transactionNumber=" + transactionNumber + ", cardNumber="
+ cardNumber + ", dateAdded=" + dateAdded + ", callingEntity=" + callingEntity + ", accountId="
+ accountId + ", routingNumber=" + routingNumber + ", bankAccountNumber=" + bankAccountNumber
+ ", comments=" + comments + ", userId=" + userId + ", removeDate=" + removeDate + "]";
}
}
My Service Class:
Autowiring the DAO instance inside my Service Class:
Implementing the DAO instance inside a Method in the Service Class:
private void fraudCheck(PaymentDetail paymentDetail) throws RegularPaymentBusinessException {
logger.info("INSIDE FRAUD CHECK METHOD");
String pmtInd=paymentDetail.getPmtInd();
logger.info("pmtInd: " + pmtInd);
String tokenizedCardNum=paymentDetail.getTokenizedCardNum();
logger.info("tokenizedCardNum: " + tokenizedCardNum);
if(pmtInd.equalsIgnoreCase(VepsConstants.GIFT_CARD_IDENTIFIER) || pmtInd.equalsIgnoreCase(VepsConstants.CREDIT_CARD_IDENTIFIER) || pmtInd.equalsIgnoreCase(VepsConstants.DEBIT_CARD_IDENTIFIER)) {
logger.info("INSIDE CARD CHECK");
TFraudCard fraudCard = authPaymentDao.findByTokenNumber(tokenizedCardNum);
logger.info("fraudCard Details: " + fraudCard.toString());
if(fraudCard!=null) {
logger.info("INSIDE EXCEPTION FLOW FOR CARD FRAUD CHECK");
throw new RegularPaymentBusinessException(VepsConstants._9966, VepsConstants._9966_MESSAGE, VepsConstants.FAILURE);
}
}
}
Even though I pass the same token Number (tokenizedCardNumber) in my method as the data in the TOKEN_NUMBER column of my TFRAUDCARD table I still get a NullPointerException when I try to print a toString() of the Entity Object.
Here is the NullPointerException on my cloudFoundry logs (Click on it to see zoomed image) :
I'm providing the DB details in my dev properties file:
I have gone over every scenario in my head for why it breaks but I still can't come up with an answer. I'm using my variable marked with #Id i.e. the Primary Key for my find() method in the Repository.
I'm also adding a #Query annotation just to be even more specific.
It still does not work.

Not-null property references a transient value - transient instance must be saved before current operation

This is the log exception:
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/Libreria] threw exception [Request processing failed; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : com.angelo.springmvc.model.PrestitoLibro.prestito -> com.angelo.springmvc.model.Prestito] with root cause
org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : com.angelo.springmvc.model.PrestitoLibro.prestito -> com.angelo.springmvc.model.Prestito
at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:137)
at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:318)
at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:658)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:813)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
at com.angelo.springmvc.dao.AbstractDao.persist(AbstractDao.java:34)
at com.angelo.springmvc.dao.PrestitoLibroDaoImpl.savePrestitoLibro(PrestitoLibroDaoImpl.java:27)
at com.angelo.springmvc.service.PrestitoServiceImpl.salvaPrestito(PrestitoServiceImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy45.salvaPrestito(Unknown Source)
at com.angelo.springmvc.controller.ShoppingCartController.ordinaShoppingCart(ShoppingCartController.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
The scenario is the following: I have two entity beans Prestito and PrestitoLibro, a ServicePrestito and a session bean ShoppingCard with a Controller:
#Entity
#Table(name="PRESTITO")
#XmlRootElement
public class Prestito {
private Integer id;
private Cliente cliente;
private Date dataprestito;
private Set<PrestitoLibro> prestitolibros = new HashSet<PrestitoLibro>(0);
public Prestito() {
}
public Prestito(Cliente cliente, Date dataprestito) {
this.cliente = cliente;
this.dataprestito = dataprestito;
}
public Prestito(Cliente cliente, Date dataprestito, Set<PrestitoLibro> prestitolibros) {
this.cliente = cliente;
this.dataprestito = dataprestito;
this.prestitolibros = prestitolibros;
}
#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 = "id_cliente", nullable = false)
public Cliente getCliente() {
return this.cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
#Temporal(TemporalType.DATE)
#Column(name = "dataprestito", nullable = false, length = 10)
public Date getDataprestito() {
return this.dataprestito;
}
public void setDataprestito(Date dataprestito) {
this.dataprestito = dataprestito;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "prestito")
public Set<PrestitoLibro> getPrestitolibros() {
return this.prestitolibros;
}
public void setPrestitolibros(Set<PrestitoLibro> prestitolibros) {
this.prestitolibros = prestitolibros;
}
#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;
Prestito other = (Prestito) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
#Override
public String toString() {
return "Prestito [id=" + id + ", cliente=" + cliente + ", dataprestito=" + dataprestito
+ ", prestitolibros=" + prestitolibros + "]";
}
}
#Entity
#Table(name="PRESTITOLIBRO")
#XmlRootElement
public class PrestitoLibro {
private Integer id;
private Libro libro;
private Prestito prestito;
private Integer qta;
private Double subtotal;
public PrestitoLibro() {
}
public PrestitoLibro(Libro libro, Prestito prestito) {
this.libro = libro;
this.prestito = prestito;
}
public PrestitoLibro(Libro libro, Prestito prestito, Integer qta, Double subtotal) {
this.libro = libro;
this.prestito = prestito;
this.qta = qta;
this.subtotal = subtotal;
}
#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)//, cascade=CascadeType.ALL
#JoinColumn(name = "id_libro", nullable = false)
public Libro getLibro() {
return this.libro;
}
public void setLibro(Libro libro) {
this.libro = libro;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_prestito", nullable = false)
public Prestito getPrestito() {
return this.prestito;
}
public void setPrestito(Prestito prestito) {
this.prestito = prestito;
}
#Column(name = "qta")
public Integer getQta() {
return this.qta;
}
public void setQta(Integer qta) {
this.qta = qta;
}
#Column(name = "subtotal", precision = 22, scale = 0)
public Double getSubtotal() {
return this.subtotal;
}
public void setSubtotal(Double subtotal) {
this.subtotal = subtotal;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PrestitoLibro other = (PrestitoLibro) obj;
if (id != other.id)
return false;
return true;
}
#Override
public String toString() {
return "PrestitoLibro [id=" + id + ", libro=" + libro + ", prestito=" + prestito + ", qta=" + qta
+ ", subtotal=" + subtotal + "]";
}
}
#Component
#Scope(value=WebApplicationContext.SCOPE_SESSION,
proxyMode=ScopedProxyMode.TARGET_CLASS)
public class ShoppingCart {
private Map<Libro, Integer> contents = new HashMap<>();
public Map<Libro, Integer> getContents() {
return contents;
}
public void setContents(Map<Libro, Integer> contents) {
this.contents = contents;
}
public void addLibro(Libro libro, int count){
if(contents.containsKey(libro)){
contents.put(libro, contents.get(libro)+count);
}else{
contents.put(libro,count);
}
}
public void removeLibro(Libro libro){
contents.remove(libro);
}
public void clearCart(){
contents.clear();
}
#Override
public String toString() {
return "ShoppingCart [contents=" + contents + "]";
}
public double getTotalCost(){
double totalcost = 0;
int i = 0;
for(Libro libro: contents.keySet()){
i = contents.get(libro);
totalcost += i * libro.getPrezzo();
}
return totalcost;
}
}
The problem occurs when I call salvaprestito() here:
#Service("prestitoService")
#Transactional
public class PrestitoServiceImpl implements PrestitoService {
#Autowired
private PrestitoDao dao;
#Autowired
private PrestitoLibroDao daoprestitolibro;
public Prestito findById(int id) {
return dao.findById(id);
}
public void savePrestito(Prestito prestito) {
dao.savePrestito(prestito);
}
public void updatePrestito(Prestito prestito) {
Prestito entity = dao.findById(prestito.getId());
if(entity!=null){
entity.setCliente(prestito.getCliente());
entity.setDataprestito(prestito.getDataprestito());
entity.setPrestitolibros(prestito.getPrestitolibros());
}
}
public void deletePrestitoBySss(String sss) {
dao.deletePrestitoBySss(sss);
}
public List<Prestito> findAllPrestiti() {
return dao.findAllPrestiti();
}
public Prestito findPrestitoBySss(String sss) {
return dao.findPrestitoBySss(sss);
}
public boolean isPrestitoSssUnique(Integer id, String sss) {
Prestito prestito = findPrestitoBySss(sss);
return ( prestito == null || ((id != null) && (prestito.getId() == id)));
}
public void salvaPrestito(Map<Libro, Integer> shoppingcartContents, Cliente cliente){
Prestito prestito = new Prestito();
prestito = dao.findById(prestito.getId());
prestito.setCliente(cliente);
dao.savePrestito(prestito);
for(Entry<Libro, Integer> entry: shoppingcartContents.entrySet()){
PrestitoLibro prestLibro = new PrestitoLibro(entry.getKey(),prestito);
prestLibro.setQta(entry.getValue());
prestLibro.setPrestito(prestito); prestLibro.setSubtotal(entry.getKey().getPrezzo()*entry.getValue());
daoprestitolibro.savePrestitoLibro(prestLibro);
prestito.getPrestitolibros().add(prestLibro);
}
}
And this is the Shopping Cart Controller:
#Controller
public class ShoppingCartController {
private static final Logger logger = LoggerFactory.getLogger(ShoppingCartController.class);
#Autowired
private LibroService libroservice;
#Autowired
private PrestitoService prestitoservice;
#Autowired
private ShoppingCart shoppingcart;
#RequestMapping(value = { "/shoppingcart/add/{libroId}" }, method = RequestMethod.GET)
public String addtoShoppingCart(#PathVariable("libroId") int libroId, #RequestHeader("referer") String rForm) {
Libro libro = (Libro) libroservice.findById(libroId);
shoppingcart.addLibro(libro, 1);
logger.debug("Aggiunto Libro a carrello" + libro);
return "redirect:" + rForm;
}
#RequestMapping(value = { "/shoppingcart" }, method = RequestMethod.GET)
public String viewShoppingCart(Model model) {
model.addAttribute("shoppingcart", shoppingcart);
return "shoppingcart";
}
#RequestMapping(value = { "/shoppingcart/order" }, method = RequestMethod.POST)
public String ordinaShoppingCart(HttpSession session) {
if(shoppingcart.getContents().isEmpty()){
return "redirect:/shoppingcart";
}else{
Cliente cliente = (Cliente) session.getAttribute("cliente");
prestitoservice.salvaPrestito(shoppingcart.getContents(), cliente);
return "redirect:/shoppingcart";
}
}
These are the Daos:
#Repository("prestitolibroDao")
public class PrestitoLibroDaoImpl extends AbstractDao<Integer, PrestitoLibro> implements PrestitoLibroDao{
public PrestitoLibro findById(int id) {
PrestitoLibro prestitolibro = getByKey(id);
if(prestitolibro!=null){
Hibernate.initialize(prestitolibro.getId());
}
return prestitolibro;
}
public void savePrestitoLibro(PrestitoLibro prestitolibro) {
persist(prestitolibro);
}
public void deletePrestitoLibroById(int id) {
Query query = getSession().createSQLQuery("delete from Prestitolibro where id = :id");
query.setString("id",(String) Integer.toString(id));
query.executeUpdate();
}
#SuppressWarnings("unchecked")
public List<PrestitoLibro> findAllPrestitiLibro() {
Criteria criteria = createEntityCriteria();
return (List<PrestitoLibro>) criteria.list();
}
#SuppressWarnings("unchecked")
public List<PrestitoLibro> findAllPrestitiLibroByPrestito(int prestitoId) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("id_prestito", prestitoId));
return (List<PrestitoLibro>) criteria.list();
}
#SuppressWarnings("unchecked")
public List<PrestitoLibro> findAllPrestitiLibroByLibro(int libroId) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("id_libro", libroId));
return (List<PrestitoLibro>) criteria.list();
}
#Repository("prestitoDao")
public class PrestitoDaoImpl extends AbstractDao<Integer, Prestito> implements PrestitoDao{
public Prestito findById(int id) {
Prestito prestito = getByKey(id);
if(prestito!=null){
Hibernate.initialize(prestito.getId());
}
return prestito;
}
public void savePrestito(Prestito prestito) {
persist(prestito);
// Save prestito significa salvare il prestito o/e anche PrestitoLibiri
}
public void deletePrestitoBySss(String sss) {
Query query = getSession().createSQLQuery("delete from Libro where snn = :snn");
query.setString("sss", sss);
query.executeUpdate();
}
#SuppressWarnings("unchecked")
public List<Prestito> findAllPrestiti() {
Criteria criteria = createEntityCriteria();
return (List<Prestito>) criteria.list();
}
public Prestito findPrestitoBySss(String sss) {
System.out.println("SSS : "+sss);
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("sss", sss));
Prestito prestito = (Prestito)criteria.uniqueResult();
if(prestito!=null){
Hibernate.initialize(prestito.getId());
}
return prestito;
}
Hibernate needs only id in Prestito to associate it with PrestitoLibro by a foreign key. I think, you don't have id in prestito here
prestLibro.setPrestito(prestito);
daoprestitolibro.savePrestitoLibro(prestLibro);
Try to output prestito.getId() before set it to prestLibro.
If you really don't have id the reason may be with this generation strategy
#GeneratedValue(strategy = IDENTITY)
or you don't have a transaction with #Transactional in the PrestitoServiceImpl so dao.savePrestito(prestito) doesn't save prestito at all. May be adding of your spring xml configuration in your question let to help you.
You do some unnecessary stuff in your method so it should be
public void salvaPrestito(Map<Libro, Integer> shoppingcartContents, Cliente cliente){
Prestito prestito = new Prestito();
prestito.setCliente(cliente);
dao.savePrestito(prestito);
for(Entry<Libro, Integer> entry: shoppingcartContents.entrySet()){
PrestitoLibro prestLibro = new PrestitoLibro(entry.getKey(), prestito);
prestLibro.setQta(entry.getValue());
prestLibro.setSubtotal(entry.getKey().getPrezzo() * entry.getValue());
daoprestitolibro.savePrestitoLibro(prestLibro);
}
}
You don't need prestLibro.setPrestito(prestito) and
prestito.getPrestitolibros().add(prestLibro).

Resources