如何使用组成的外键从实体中检索数据?

发布于 2025-01-24 07:15:39 字数 1863 浏览 0 评论 0原文

我无法从Oracle数据库中从实体中检索数据。

配置:

  • Java 8
  • Hibernate 5.4.20
  • JPA 2.2
  • Oracle11g

我的情况:

JPA实体赔偿,带有“经典”数据,以及来自车辆的ID。 JPA实体车辆存储而没有唯一的ID列,因为在我的桌子中,每个车辆都有具有独特属性的时期(通过StartDate和Enddate识别)。

示例:

ID情况开始日期
1en_usine01/01/202014/01/2020
1Transfert15/01/202017/01/2020
1EN_CONCESSION 1 EN_CONCESSION18/01/202001/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 :

idsituationstartDateendDate
1EN_USINE01/01/202014/01/2020
1TRANSFERT15/01/202017/01/2020
1EN_CONCESSION18/01/202001/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-01-31 07:15:39

首先是,如果您有一个由多个字段组成的ID,那么您就不能没有提供任何@ID,其次上课为班级成员,以下是一个例子:

@Embeddable 
public class MyComposedID {
 
//Here ,I personaly don't see the usage of id it's neither a surrogate key nor
//used to have any impact on the uniqueness of the Composed Id since you said 
//it's uniquely identified by the start and end date
 @Column(name="id")
 String id;
 @Column(name="start_date")
 LocalDate startDate;

 @Column(name="end_date")
 LocalDate endDate;
 
}

现在您将其声明为嵌入式嵌入式,如下所示:

@Entity
Public class Vehicule {

 @EmbeddedId
 MyComposedID id;

}

至于查询,从我所理解的情况下,您想在车辆之间获取赔偿的车辆。 。开始和车辆。
以下是JPQL查询的示例:

Query query = em.createQuery("SELECT v FROM Vehicle v "+
       "INNER JOIN Reparation r ON r.reparationDate>= v.id.startDate "+
       "AND r.reparationDate<=v.id.endDate");

希望这对您有所帮助。

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 :

@Embeddable 
public class MyComposedID {
 
//Here ,I personaly don't see the usage of id it's neither a surrogate key nor
//used to have any impact on the uniqueness of the Composed Id since you said 
//it's uniquely identified by the start and end date
 @Column(name="id")
 String id;
 @Column(name="start_date")
 LocalDate startDate;

 @Column(name="end_date")
 LocalDate endDate;
 
}

Now you declare it in your Vehicle Entity as an EmbeddedID as follows :

@Entity
Public class Vehicule {

 @EmbeddedId
 MyComposedID id;

}

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 :

Query query = em.createQuery("SELECT v FROM Vehicle v "+
       "INNER JOIN Reparation r ON r.reparationDate>= v.id.startDate "+
       "AND r.reparationDate<=v.id.endDate");

Hope this helps you .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文