使用CrudRepository接口自动实现返回不同类型对象的方法

时间:2017-03-26 13:03:35

标签: java spring-mvc spring-boot

我正在尝试将Spring MVC应用程序转换为Spring REST + Angular2应用程序的组合。这是我的' Ticket.java'实体类(跳过getter和setter)

@Entity
@Table(name="ticket")
public class Ticket {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;

@ManyToOne
@JoinColumn(name="bookings_id")
private Booking booking;

@ManyToOne
@JoinColumn(name="customer_id")
private Customer customer;

@Column(name="seat_no")
private int seatNumber;



public Ticket(){

}

如何在TicketDAO中编写一个方法,返回给出booking.id的所有客户?这是TicketDAO.java接口

  public interface TicketDAO extends CrudRepository<Ticket, Integer>{

    // I want to auto-implement such type of method using CrudRepository
//  public List<Customer>  getCustomersBooking(int bId);    }

我之前已经实现过以下方法:

@Override
    public List<Customer> getCustomersBooking(int bId) {


      Session currentSession = sessionFactory.getCurrentSession();

        logger.info("DAOgetCustomersBooking: D1");
      List<Customer>  customer = new ArrayList<Customer>();

        Query<Ticket> theQuery =
                currentSession.createQuery("from Ticket where bookings_id = "+bId, Ticket.class);

        List<Ticket>  tickets = theQuery.getResultList();
            for (Ticket temp: tickets){
                customer.add(temp.getCustomer());
                }

        return customer;
    }

但是现在我想在TicketDAO界面中使用CrudRepository自动实现这种类型的方法。我将如何编写一个能让我这样做的方法声明?

供参考,Booking.java

@Entity
@Table(name="bookings")
public class Booking {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;

    @ManyToOne
    @JoinColumn(name="van_id")
    private Van van;

    @ManyToOne
    @JoinColumn(name="driver_id")
    private Driver driver;

    @ManyToOne
    @JoinColumn(name="route_id")
    private Route route;

    @Column(name="registered_seats")
    private int registeredSeats;

    @Column(name="departure_time")
    private String departureTime;
    @Column(name="arival_time")
    private String arrivalTime;
    @Column(name="departure_date")
    private String departureDate;



    @Column(name="expected_price")
    private int expectedPrice;

    //Ticket.java   reference
     @OneToMany(mappedBy="booking",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
     private Set<Ticket> tickets;


    //Webdata.java   reference
         @OneToOne(mappedBy="bookingWebdata",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
            private Webdata webdata;

Customers.java

@Entity
@Table(name="customer")
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

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

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

    @Column(name="password")
    private String password;

    @Column(name="regnumber")
    private int regNumber;

    @Column(name="phonenumber")
    private int phoneNumber;

    @Column(name="flagged")
    private int flagged;


    //Ticket.java   reference
         @OneToMany(mappedBy="customer",cascade=CascadeType.ALL)
         private Set<Ticket> tickets;

1 个答案:

答案 0 :(得分:2)

对于我使用JpaRepository的项目,但我认为这是同一种请求。

你必须根据你的java实体使用带有HQL的@Query()。

我直接在请求中提供了对象而不是id,所以我不知道你是否可以像对象那样使用id。 像这样:

public interface TicketDAO extends CrudRepository<Ticket, Integer>{

    @Query("SELECT t.customer FROM Ticket t WHERE t.booking = ?1")
    public List<Customer>  getCustomersBooking(Booking booking);    
}

如果你想尝试使用id,那就是:

public interface TicketDAO extends CrudRepository<Ticket, Integer>{

    @Query("SELECT t.customer FROM Ticket t WHERE t.booking.id = ?1")
    public List<Customer>  getCustomersBooking(int bId);    
}