Java 无法在spring boot 2.5.0中获取关联的结果
我正在尝试将我的spring boot项目从2.4.3升级到2.5.0。发生在我身上的奇怪事情是,当实体关联时,我无法获取结果 例如,我有两个简单的实体: 用户配置文件实体:Java 无法在spring boot 2.5.0中获取关联的结果,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试将我的spring boot项目从2.4.3升级到2.5.0。发生在我身上的奇怪事情是,当实体关联时,我无法获取结果 例如,我有两个简单的实体: 用户配置文件实体: @Entity @Table(name = "user_profile") public class UserProfile { @Id private String id; @Column(name = "first_name") private String firstNam
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
private String id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String email;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "tenantId")
private Tenant tenant;
public Tenant getTenant() {
return tenant;
}
public void setTenant(Tenant tenant) {
this.tenant = tenant;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
承租人实体:
@Entity
public class Tenant {
@Id
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
UserProfile实体与租户实体具有多对一关联。我的存储库类是
public interface UserProfileRepository extends CrudRepository<UserProfile, String> {
UserProfile findByEmailAndTenantId(String email, String tenantId);
}
spring boot 2.4.3为相同实体生成的SQL为
select
userprofil0_.id as id1_1_,
userprofil0_.email as email2_1_,
userprofil0_.first_name as first_na3_1_,
userprofil0_.last_name as last_nam4_1_,
userprofil0_.tenant_id as tenant_i5_1_
from
pr.user_profile userprofil0_
where
userprofil0_.email=?
and (
userprofil0_.tenant_id is null
)
这是SpringBoot2.5.0中的预期行为吗?有人能帮我找到这个问题的解决方案吗?我在文档中找不到任何关于这个问题的信息,但请阅读这个 似乎
optional=false
会导致内部连接到租户,与租户id为NULL的相结合,不能返回任何行
因此,如果您确实需要获取tenant\u id
设置为null的实体,您必须从@JoinColumn
注释中删除optional=false
。您能将“spring.jpa.show sql”设置为true并粘贴结果吗?另外,您的联接列名似乎不合适,user_profile
表中是否有列名为tenantId
或tenant_id
?@KamilBęben,谢谢您的回复。我用sql查询编辑了这个问题。
select
userprofil0_.id as id1_1_,
userprofil0_.email as email2_1_,
userprofil0_.first_name as first_na3_1_,
userprofil0_.last_name as last_nam4_1_,
userprofil0_.tenant_id as tenant_i5_1_
from
pr.user_profile userprofil0_
where
userprofil0_.email=?
and (
userprofil0_.tenant_id is null
)