英文:
Spring jpa+hibernate returns empty list in @OneToMany relation
问题
I have created 2 Entity classes:
@MappedSuperclass
class BaseEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
var id: Long,
@Column(name = "created", nullable = false)
var created: Instant,
@Column(name = "updated", nullable = false)
var updated: Instant
) {
}
@Entity
@Table(name = "users")
class User(
id: Long = 0,
@Column(name = "name", nullable = false)
var name: String,
@Column(name = "email", nullable = false)
val email: String,
created: Instant = Instant.now(),
updated: Instant = Instant now(),
@Enumerated(EnumType.STRING)
var status: UserStatus = UserStatus.NOT_CONFIRMED,
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
val auths: MutableList<UserAuth>
) : BaseEntity(id, created, updated)
@Entity
@Table(name = "auths")
class UserAuth(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0,
@Column(name = "status")
@Enumerated(EnumType.STRING)
var status: UserStatus = UserStatus.ACTIVE,
@Column(name = "auth_info")
@JdbcTypeCode(SqlTypes.JSON)
var auth: BaseAuth,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
val user: User,
created: Instant = Instant.now()
) : BaseAuth(created) {
}
When trying to get a value that has already been saved in the database (using UserRepository.findById(...)
), it returns an empty list (for User.auths
). I have checked that UserAuth
is connected to User
when trying to find.
Also, it's marked for the plugin all-open
in the Gradle file:
allOpen {
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.MappedSuperclass")
annotation("jakarta.persistence.Embeddable")
}
I use Liquibase for migrations, and when I set spring.jpa.hibernate.ddl-auto: validate
(it throws an exception about a schema error) - but it is configured to none (as recommended in all tutorials).
I have tried to add @Join(table="auths")
, @Fetch(FetchMode.SELECT/SUBSELECT)
, @JoinColumn("user_id")
, and it caused no result at all.
How to fix this problem?
英文:
I have created 2 Entity classes:
@MappedSuperclass
class BaseEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
var id: Long,
@Column(name = "created", nullable = false)
var created: Instant,
@Column(name = "updated", nullable = false)
var updated: Instant,
) {
}
@Entity
@Table(name = "users")
class User(
id: Long = 0,
@Column(name = "name", nullable = false)
var name: String,
@Column(name = "email", nullable = false)
val email: String,
created: Instant = Instant.now(),
updated: Instant = Instant.now(),
@Enumerated(EnumType.STRING)
var status: UserStatus = UserStatus.NOT_CONFIRMED,
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
val auths: MutableList<UserAuth>
) : BaseEntity(id, created, updated)
@Entity
@Table(name = "auths")
class UserAuth(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0,
@Column(name = "status")
@Enumerated(EnumType.STRING)
var status: UserStatus = UserStatus.ACTIVE,
@Column(name = "auth_info")
@JdbcTypeCode(SqlTypes.JSON)
var auth: BaseAuth,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
val user: User,
created: Instant = Instant.now()
) : BaseAuth(created) {
}
When trying to get value that already had been saved in database (by UserRepository.findById(...)) it returns empty list (at User.auths). I have checked that UserAuth is connected to User when trying to find.
Also it marked for plugin all-open in gradle file:
allOpen {
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.MappedSuperclass")
annotation("jakarta.persistence.Embeddable")
}
I use liquidbase for migrations, and when I set spring.jpa.hibernate.ddl-auto:validate (it throws exception about schema error) - but it configured to none (as recomended in all tutorials)
I have tried to add @Join(table="auths"), @Fetch(FetchMode.SELECT/SUBSELECT),@JoinColumn("user_id") and it's caused no result at all.
How to fix this problem?
答案1
得分: 0
我找到了问题的原因:Hibernate有一级缓存。如果我尝试获取刚刚保存的实体,将不会发出对数据库的请求,而是返回缓存中的实体。
英文:
I found reason of my problem: hibernate has first level cache. If I am trying to get an entity that I have just saved, the request to the database will not be made and the entity from the cache will be returned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论