There are two papers and parts in the project. They are one-to-many. I always get the result I want through JPA’s findById, but this time, In the case that the correlation between paper and part is normal, it still makes me feel very magical that the query can’t come out.
Looking for problems
At first, I looked through the code, but I didn’t see anything wrong with it. Since the query failed, how about the generated SQL statement? Application
. Spring: jpa: hibernate: # show SQL: ${show-sql:true} properties: hibernate: # format SQL format_sql: true....
The SQL generated when querying the test paper with findByID is as follows
Hibernate: select paper0_.id as id1_11_, ...... from paper paper0_ where paper0_.id=? and paper0_.deleted=0 .... Hibernate: select parts0_.paper_id as paper_id5_13_0_, ...... from part parts0_ inner join course course1_ on parts0_.course_id=course1_.id ...... where parts0_.paper_id=? .
It turns out that the Course and the Part of the exam paper are connected by an inner join
inner join
In the case of Inner Join, both parties must have the data, that is to say, in the above example, part must have associated data with course before it can be queried, is that the reason? The foreign key of part to couse is empty.
This is just a brief introduction to Inner Join. If you don’t know about it, you can read this article by your senior
Why is it an inline link
Why is it an inner link? If we go to the file that defines the Part entity, we will find that there is a constraint
@JoinColumn(nullable = false)
The annotation defines that the foreign key will not be NULL, so an INNER JOIN is used. At the same time, since the field was added later via flyway, but not defined as NOT NULL in the database structure, both of these create the problem of being able to insert but not to query
@NotNull vs @Column(nullable = false)
@column (nullable = false) and @joincolumn (nullable = false) are the same to me, just a regular Column (Long, String……). , one on an object that has a foreign key relationship, so only one of them.
@NotNULL is the JSR 303 Bean Validation annotation. It has nothing to do with the database constraint itself. Since Hibernate is the reference implementation of JSR 303, it intelligently chooses these constraints and translates them into database constraints, so you have two constraints. @Column(nullable = false) is the JPA method for declaring the Column to be non-empty. That is, the former is used for validation, while the latter is used to indicate database schema details.
JSR stands for Java Specification Requests. Is pointing to
JCP(Java Community Process) makes a formal request to add a standardized technical specification. Anyone can submit a JSR to add new APIs and services to the Java platform. The JSR has become an important standard in the Java world.
Take the @JoinColumn (nullable = false) example above. If we use @NotNull here, we will be told that we cannot save successfully even though there is no constraint in the database
So, @NotNull is a better choice than @Column(nullable = false) when we only need validation. In addition, if we want to use @NotNull to enable Hibernate to generate database constraints for us, We need to ensure that * * hibernate. The validator. Apply_to_ddl_ = true (the default is true) * *
Copyright statement
The author:
Hebei University of Technology Mengyunzhi Development Team –
Yi-heng lee
Refer to the article
Confusion: @NotNull vs. @Column(nullable = false) with JPA and Hibernate
Hibernate @NotNull vs @Column(nullable = false)
JSR-303