我正在学习本教程,以了解如何在NetBeans CRUD应用程序中使用Java DB: https://platform.netbeans.org/tutorials/nbm-crud.html
我已经达到了运行应用程序的程度。
问题是:为什么产出说:
warning: [options] bootstrap class path not set in conjunction with -source 1.7
warning: No processor claimed any of these annotations: javax.annotation.Generated
C:\Users\YonePC\Documents\NetBeansProjects\DBManager\CustomerViewer\src\org\shop\viewer\CustomerViewerTopComponent.java:51: warning: [unchecked] unchecked cast
List<Customer> resultList = (List<Customer>) query.getResultList();
required: List<Customer>
found: List
2 warnings
我们正在尝试的代码以及错误在哪里:
public CustomerViewerTopComponent() {
initComponents();
setName(Bundle.CTL_CustomerViewerTopComponent());
setToolTipText(Bundle.HINT_CustomerViewerTopComponent());
EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager();
Query query = entityManager.createNamedQuery("Customer.findAll");
List<Customer> resultList = query.getResultList();
for (Customer c : resultList) {
jTextArea1.append(c.getName() + " (" + c.getCity() + ")" + "\n");
}
}
客户是实体类:
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "CUSTOMER_ID")
private Integer customerId;
@Column(name = "NAME")
private String name;
@Column(name = "ADDRESSLINE1")
private String addressline1;
@Column(name = "ADDRESSLINE2")
private String addressline2;
@Column(name = "CITY")
private String city;
@Column(name = "STATE")
private String state;
@Column(name = "PHONE")
private String phone;
@Column(name = "FAX")
private String fax;
@Column(name = "EMAIL")
private String email;
@Column(name = "CREDIT_LIMIT")
private Integer creditLimit;
@JoinColumn(name = "DISCOUNT_CODE", referencedColumnName = "DISCOUNT_CODE")
@ManyToOne(optional = false)
private DiscountCode discountCode;
@JoinColumn(name = "ZIP", referencedColumnName = "ZIP_CODE")
@ManyToOne(optional = false)
private MicroMarket zip;
... getters and setters ...
我尝试了以下内容:
public final class CustomerViewerTopComponent extends TopComponent {
public CustomerViewerTopComponent() {
initComponents();
setName(Bundle.CTL_CustomerViewerTopComponent());
setToolTipText(Bundle.HINT_CustomerViewerTopComponent());
EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager();
Query query = entityManager.createNamedQuery("Customer.findAll");
List<Customer> resultList = (List<Customer>) query.getResultList();
for (Customer c : resultList) {
jTextArea1.append(c.getName() + " (" + c.getCity() + ")" + "\n");
}
}
我尝试强制转换List,将给定的输入与List String匹配,并将其声明为:List String
你能帮我吗?
答案 0 :(得分:2)
这不是一个错误。这是一个简单的编译器警告。它告诉你,在运行时,不会检查列表确实是List<Customer>
。
禁止警告的正确方法是通过调用使用TypedQuery
createNamedQuery("Customer.findAll", Customer.class)
。这不会保证列表确实在运行时包含客户,但它会抑制警告。
编译器没有发现代码中的主要问题:不应该使用Swing组件中的EntityManager。这应该委托给另一个对象。每次需要执行查询时创建一个新的EntityManagerFactory实际上并不是一个好主意。 EntityManagerFactory是一个重量级,线程安全的对象。你应该只创建一次,并在你需要的地方使用它。