10

I'm trying to save a nested object using hibernate and I receive could not execute statement; SQL [n/a] Exception

CODE

@Entity
@Table(name = "listing")
@Inheritance(strategy = InheritanceType.JOINED)
public class Listing implements Serializable {

  @Id
  @Column(name = "listing_id")
  private String listingId;

  @Column(name = "property_type")
  private PropertyType propertyType;

  @Column(name = "category")
  private Category category;

  @Column(name = "price_currency")
  private String priceCurrency;

  @Column(name = "price_value")
  private Double priceValue;

  @Column(name = "map_point")
  private MapPoint mapPoint;

  @Column(name = "commission_fee_info")
  private CommissionFeeInfo commissionFeeInfo;
}


public class MapPoint implements Serializable {

  private final float latitude;
  private final float longitude;
}

public class CommissionFeeInfo implements Serializable {

  private String agentFeeInfo;
  private CommissionFeeType commissionFeeType;
  private Double value;
  private Double commissionFee;
}

public enum CommissionFeeType implements Serializable { }

Using RazorSQL I saw that hibernate defines MapPoint and CommissionFee as VARBINARY

What I can't understand, is the fact that hibernate manages to save it when commissionFeeInfo is not present. It has no problem with saving MapPoint

Does anyone have an idea about what I do wrong?

UPDATE

I found out that if all attributes of CommissionFeeInfo excepting agentFeeInfoare null, the object will be saved without problems. If one of the other attributes is != null, the errors occur.

UPDATE 2

I changed the type of all attributes of CommissionFeeInfo into String and the object will be saved without problem, but I can't let the attributes as String.

3
  • Can you explain more the error you have? There are any nested exception? Any SQL error code? Commented Aug 5, 2015 at 13:06
  • @RicardoVila yes, there's a nested exceptions java.sql.SQLDataException: data exception: string data, right truncation; table: LISTING column: COMMISSION_FEE_INFO Commented Aug 6, 2015 at 7:38
  • and also org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement and sql = n/a Commented Aug 6, 2015 at 7:44

5 Answers 5

7

I solved the problem by adding setting

@Column(name = "commission_fee_info", columnDefinition = "LONGVARBINARY")

as annotation for the field commisionFeeInfo in the class Listing

Sign up to request clarification or add additional context in comments.

Comments

3

For me,

@Column(columnDefinition="text")

solves my problem.

Comments

0

That solution could help for a different reason. One other reason could be Column length. Check your column length. I had the same error the reason was my data exceed the size of the column.

setSignInProvider("String length > 15 ") 

Before

    @Column(name = "sing_in_provider", length = 15)

and then

   @Column(name = "sing_in_provider", length = 100)

Comments

0

I was also facing the same issue . and then I solved the problem

spring.jpa.hibernate.ddl-auto=update

Comments

0

For me I'm using current_date for a field in my sql table. but this is a keyword in SQL so I can't use this name. I changed the field name to current_date_and_time it works for me. also I added the column name on my entity class.

@Column(name = "current_date_and_time")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.