9

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.

Paul
  • 1,325
  • 2
  • 19
  • 41
  • Can you explain more the error you have? There are any nested exception? Any SQL error code? – Ricardo Vila Aug 05 '15 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` – Paul Aug 06 '15 at 07: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` – Paul Aug 06 '15 at 07:44

5 Answers5

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

Paul
  • 1,325
  • 2
  • 19
  • 41
2

For me,

@Column(columnDefinition="text")

solves my problem.

SHIVA
  • 646
  • 1
  • 8
  • 13
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)
i.karayel
  • 4,377
  • 2
  • 23
  • 27
0

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

spring.jpa.hibernate.ddl-auto=update

Akin Okegbile
  • 1,108
  • 19
  • 36
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")
jerald jacob
  • 613
  • 1
  • 4
  • 18