如何使用组成的外键从实体中检索数据?
我无法从Oracle数据库中从实体中检索数据。
配置:
- Java 8
- Hibernate 5.4.20
- JPA 2.2
- Oracle11g
我的情况:
JPA实体赔偿,带有“经典”数据,以及来自车辆的ID。 JPA实体车辆存储而没有唯一的ID列,因为在我的桌子中,每个车辆都有具有独特属性的时期(通过StartDate和Enddate识别)。
示例:
ID | 情况 | 开始 | 日期 |
---|---|---|---|
1 | en_usine | 01/01/2020 | 14/01/2020 |
1 | Transfert | 15/01/2020 | 17/01/2020 |
1 | EN_CONCESSION 1 EN_CONCESSION | 18/01/2020 | 01/03/2020 |
VEARICUEL的主要键是由车辆的主要键3列:ID/StartDate/EndDate。
我想从实体赔偿的车辆中获取数据,我在桌子上得到了车辆的ID,并且inoverure是通过赔偿日期进行的,该日期必须在桌上车辆中的Startdate和端端之间进行。 我该怎么做?
@Entity
Public class Vehicule {
@Column(name="id")
String id
@Column(name="startDate")
LocalDate startDate
@Column(name="endDate")
LocalDate endDate
}
@Entity
Public class Reparation {
@Column(name="id_reparation")
String idReparation
@Column(name="date_of_reparation")
LocalDate dateOfReparation
Vehicule vehicule
}
我尝试了@joinformula,但我没有得到它,
你会怎么做?
编辑 : 我尝试了很多事情,例如:
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "select id from table_vehicule", referencedColumnName = "ID")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "select startDate from table_vehicule where dateOfReparation > startDate", referencedColumnName = "startDate")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "select endDate from table_vehicule where dateOfReparation < endDate", referencedColumnName = "endDate"))
})
Vehicule vehicule;
I can't retrieve data from an Entity from my Oracle database.
Config :
- Java 8
- Hibernate 5.4.20
- Jpa 2.2
- Oracle11g
My situation :
A JPA Entity Reparation with "classic" data, and an ID from a Vehicule.
A JPA Entity Vehicule stored without a unique ID column because in my table, each vehicule has a period (identify by startDate and endDate) with unique properties.
Example :
id | situation | startDate | endDate |
---|---|---|---|
1 | EN_USINE | 01/01/2020 | 14/01/2020 |
1 | TRANSFERT | 15/01/2020 | 17/01/2020 |
1 | EN_CONCESSION | 18/01/2020 | 01/03/2020 |
The primary key of Vehicule is defined by 3 columns : id/startDate/endDate.
I want to get data from Vehicule in the entity Reparation, i got the id of the Vehicule in the table Raparation, and the jointure is done with the date of Reparation that has to be between startDate and endDate in the table Vehicule.
How can i do that ?
@Entity
Public class Vehicule {
@Column(name="id")
String id
@Column(name="startDate")
LocalDate startDate
@Column(name="endDate")
LocalDate endDate
}
@Entity
Public class Reparation {
@Column(name="id_reparation")
String idReparation
@Column(name="date_of_reparation")
LocalDate dateOfReparation
Vehicule vehicule
}
I tried the @joinFormula, but I didn't get it
How would you do that ?
Edit :
I tried a lot of things, like :
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "select id from table_vehicule", referencedColumnName = "ID")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "select startDate from table_vehicule where dateOfReparation > startDate", referencedColumnName = "startDate")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "select endDate from table_vehicule where dateOfReparation < endDate", referencedColumnName = "endDate"))
})
Vehicule vehicule;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先是,如果您有一个由多个字段组成的ID,那么您就不能没有提供任何@ID,其次上课为班级成员,以下是一个例子:
现在您将其声明为嵌入式嵌入式,如下所示:
至于查询,从我所理解的情况下,您想在车辆之间获取赔偿的车辆。 。开始和车辆。
以下是JPQL查询的示例:
希望这对您有所帮助。
First thing is ,you can't have an @Entity without providing any @Id to that class ,second if you have an ID composed of multiple fields ,like in your case here ,you make it Embeddable ,and then you declare it in your class as a class member ,here's an example :
Now you declare it in your Vehicle Entity as an EmbeddedID as follows :
As for the query goes , from what i understood ,you want to get the vehicle where the Reparation.reparationDate is between the Vehicle.startDate and Vehicle.endDate,
here's an example with a JPQL query :
Hope this helps you .