hibernate only query operations, but the actual execution of the update statement

Only recently discovered in a transaction executed query in the project, open the sql debug print was found to have an update statement. After investigation found that the value of the object is contained in the entity does not override equals and hashcode operations, ranging from suspicion hibernate objects found when comparing with the default equals, so to perform the deletion, and then insert operation.

Entity object:

@Getter
@Setter
@ToString
@Entity
public class Person extends AbstractPersistable {
    @Column
    private String name;
    @ElementCollection(targetClass = Phone.class)
    @CollectionTable
    private Set<Phone> phones;
}

Value Object:

@Getter
@Setter
@ToString
@Embeddable
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = "number")
public class Phone implements Serializable {
    @Column
    private String number;
}

Note To override equals and hashcode. Otherwise debug log is as follows:

Hibernate: select person0_.id as id1_19_, person0_.version as version2_19_, person0_.name as name3_19_ from person person0_ where person0_.name=?
Hibernate: select phones0_.person_id as person_i1_20_0_, phones0_.number as number2_20_0_ from person_phones phones0_ where phones0_.person_id=?
Person(name=333, phones=[Phone(number=34567), Phone(number=3456)])
Hibernate: update person set version=?, name=? where id=? and version=?
Hibernate: delete from person_phones where person_id=?
Hibernate: insert into person_phones (person_id, number) values (?, ?)
Hibernate: insert into person_phones (person_id, number) values (?, ?)

Spring boot attachment opening hibernate sql printing and printing method parameters:

resources added logback-spring.xml, which reads as follows:

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>
    <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG"/>
    <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>
    <logger name="org.hibernate.SQL" level="DEBUG"/>
</configuration>

 

 

 

 

 

Released six original articles · won praise 1 · views 8542

Guess you like

Origin blog.csdn.net/guangmingguangming/article/details/104885791