仅从其他表JPA查询

时间:2018-05-04 11:10:35

标签: hibernate spring-boot jpa

我想从User表中获取唯一的特定RideTransaction表记录。 到目前为止,我已经尝试了下面的查询

@Query("SELECT r FROM RideTransaction r WHERE r.user=?1 AND r.status='RIDING' OR r.status='OUTSTANDING' OR r.status='COMPLETED'")
List<RideTransaction> findRideHistoryOfUser(User user);

另一个是

@Query("SELECT r FROM RideTransaction r WHERE r.user=?1 AND r.status='RIDING' OR r.status='OUTSTANDING'")
List<RideTransaction> findAllCurrentRidingOrOutstanding(User user);

但是当我执行上述代码时,我得到的是所有RideTransaction条记录,而不是特定的User条记录。

RideTransaction表:

@Entity
public class RideTransaction {
@Id
@GenericGenerator(name = "ridetransection_id_generator", strategy = "com.greensaikil.entity.idgenerator.RideTransectionIdGenerator")
@GeneratedValue(generator = "ridetransection_id_generator")
private long id;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "USER", foreignKey = @ForeignKey(name = "FK_RideTransection_User"))
private User user;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "DROP_UP_STAND", foreignKey = @ForeignKey(name = "FK_RideTransection_Stand_DropUp"))
private Stand drop_up_stand;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "PICK_UP_STAND", foreignKey = @ForeignKey(name = "FK_Transection_Stand_PickUp"))
private Stand pick_up_stand;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "BICYCLE", foreignKey = @ForeignKey(name = "FK_Transection_Bicycle"))
private Bicycle bicycle;

private Date pick_up_time;

private Date drop_off_time;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "PICK_UP_LOCATION", foreignKey = @ForeignKey(name = "FK_RideTransection_Location_PickUp"))
private Location pick_up_location;

@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "DROP_UP_LOCATION", foreignKey = @ForeignKey(name = "FK_RideTransection_Location_DropUp"))
private Location drop_off_location;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Payment> payments = new HashSet<Payment>();

private long booking_ride_time;

private BigDecimal total_payment;

private BigDecimal remaining_outstanding;

private BigDecimal discount;

private Date booking_time;

private long total_ride_time;

private long extra_ride_time;

private BigDecimal total_rental;

@Enumerated(EnumType.STRING)
private RideStatus status;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Image> image;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RideTracking> ride_tracking = new HashSet<RideTracking>();
  //setters and getters

用户表:

public class User {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", name = "USERID")
private UUID id;

@Column(name = "USERNAME")
private String username;

@Column(name = "NAME")
@NotNull
private String name;

@Column(name = "USEREMAIL")
private String email;

@Column(name = "USERPHONE")
private Long phone;
//setters and getters

Plases帮助我查询只获得User个被记录。

2 个答案:

答案 0 :(得分:0)

您正在创建一个RideTransaction列表,您需要创建一个用户列表,同样在SQL查询的开头您选择的是RideTransaction,而不是用户。

答案 1 :(得分:0)

这看起来像是一种混乱。什么是真正的桌子关系?如果User可以包含多个RideTransaction@OneToMane RideTransaction},那么您可以得到(尽管您注释了@OneToOne })。如果他不这样做,为什么要在存储库中声明List查询? 试试自己 - 什么会返回查询select * from RideTransaction where user = '{some_user_id}'