Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

java - Hibernate data truncation: Incorrect integer value: 'xACxEDx00x05srx00& when trying to save a OneToMany Relation

I have two entities with this kind of relation

EVENT

@Data
@Table(name = "Event", schema = "schema")
public class Event extends Loggable implements Serializable {
        
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
            
  //   ....
    
  @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.ALL}, fetch = FetchType.EAGER)
  List<RecurringPattern> recurringPatterns;
    
  private void setRecurringPatterns(List<RecurringPattern> children) {
     this.recurringPatterns = children;
  }
    
  public void addRecurringPattern(RecurringPattern child) {
     recurringPatterns.add(child);
     child.setEvent(this);
  }

RECURRING PATTERN

@Data
@Table(name = "RecurringPattern")
public class RecurringPattern implements Serializable {
    
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer id;
    
   @Column(name = "eventId") <-- ManyToOne here has caused problems
   private Event event;

}

In a Controller in an "one shot" operation, I try to save at the same time an Event and his RecurringPattern child, reading the data from a Json.

event = new Event();
recurringPattern = new RecurringPattern();
    
event.GetDataFromJson(...);
recurringPattern.GetDataFromJson(...);
event.addRecurringPattern(recurringPattern);
    
try {
   eventRepository.save(event);
}
catch(Exception e) {
   System.out.println(e.getMessage());
}

With the debugger everything seems fine and no Exception is thrown during the .save() method.

But then I get this kind of error in the Stacktrace:

    com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect integer value: 'xACxEDx00x05srx00&it.***.checklist.models.calendar.EventxD9x09xD1x95xF9xBFx05Fx02x00x0FLx00x09createdBytx00x1' for column `schema`.`recurringpattern`.`eventId` at row 1

I really don't get it, is it something related to my annotations or the Utf encoding of the DB?

question from:https://stackoverflow.com/questions/65904263/hibernate-data-truncation-incorrect-integer-value-xac-xed-x00-x05sr-x00-whe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should correct this:

@Column(name = "eventId") <-- ManyToOne here has caused problems
private Event event;

to this:

@ManyToOne
@JoinColumn(name = "eventId")
private Event event;

and this:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.ALL}, fetch = FetchType.EAGER)
List<RecurringPattern> recurringPatterns;

to this:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "event")
List<RecurringPattern> recurringPatterns;

See also this section of hibernate documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...