使用JPA在Vaadin网格中显示一些列

时间:2018-04-14 13:59:13

标签: java spring-boot jpa vaadin

我创建了实体类:

plt.plot(history.history['acc'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

为此添加了存储库:

package order;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.UUID;

@Entity
@Table(name = "bo_order")
public class Order {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String login;
    private Long internalNumber;
    private String food;
    @Column(name="data_ins")
    private LocalDate dateOfOrder;
    private String orderNumber;

    public Order(){}

    public Order(String login)
    {
        this.login = login;
    }

    public Order(String login, Long internalNumber, String food, LocalDate dateOfOrder) {
        this.login = login;
        this.internalNumber = internalNumber;
        this.food = food;
        this.dateOfOrder = dateOfOrder;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public String generateOrderNumber()
    {
        return UUID.randomUUID().toString();
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", login='" + login + '\'' +
                ", internalNumber=" + internalNumber +
                ", food='" + food + '\'' +
                ", dateOfOrder=" + dateOfOrder +
                '}';
    }
}

并尝试在Grid中显示来自@Query的值:

package order;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface OrderRepository extends CrudRepository<Order, Long>{
    List<Order> findByLogin(String login);

    @Query(value="SELECT id, login FROM bo_order", nativeQuery = true)
    public List<Order> findAllPurchasers();
}

但我收到错误package purchasers; import com.vaadin.annotations.Title; import com.vaadin.server.VaadinRequest; import com.vaadin.spring.annotation.SpringUI; import com.vaadin.ui.*; import order.Order; import order.OrderRepository; import org.springframework.beans.factory.annotation.Autowired; @SpringUI(path = "/allpurchasers") @Title("All today's purchasers") public class AllPurchasersGUI extends UI { @Autowired private final OrderRepository orderRepository; final Grid<Order> grid; public AllPurchasersGUI(OrderRepository pr) { this.orderRepository = pr; grid = new Grid<>(Order.class); grid.setSizeFull(); } @Override protected void init(VaadinRequest vaadinRequest) { setContent(grid); listPurchasers(); } private void listPurchasers() { grid.setItems(orderRepository.findAllPurchasers()); } } 然后

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT id, login FROM bo_order]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

我知道Caused by: org.postgresql.util.PSQLException: The column name data_ins was not found in this ResultSet. 不在ResultSet中,因为我不想在那里。我可以使用data_ins在bo_order中显示网格中的所有值,但我只需要findAllid。我怎么能得到这个?我还试图制作login而不是List<Object>,但后来显示我的问题。

2 个答案:

答案 0 :(得分:1)

TL; DR:将grid.setColumns("id", "login")添加到listPurchasers()方法的末尾,或在data_ins注释的数据库列列表中包含@Query

使用Grid构造函数创建Grid(Class<T> beanType)时,将为bean类型中的每个getter添加一列。除非从数据库加载所有这些getter的数据,否则这将导致错误。

在这种情况下,@Query注释定义只应从数据库中为列idlogin提取数据。您获得的异常是指数据库列data_ins,它似乎映射到dateOfOrder bean属性,但我没有看到任何代码会删除该属性的自动添加列

确保网格仅使用所需列的最简单方法是使用setColumns方法。该方法的工作方式与setColumnOrder类似,不同之处在于它还会删除任何未包含在参数中的列。

或者,如果您确实希望将数据显示为网格中的列,您还可以更改@Query注释以包含数据库中的data_ins属性。

答案 1 :(得分:0)

Grid从Order类获取参数,因此如果有许多getter,那么它们将显示在列中。我做了:

private void listPurchasers()
{
    List<Order> allOrders = (List<Order>) orderRepository.findAll();
    grid.setItems(allOrders);
    grid.setColumnOrder("id", "login", "dateOfOrder");
    grid.removeColumn("food");
}

因此,添加/删除要显示的列似乎非常容易。