我正在做以下事情:
@Entity
@SqlResultSetMapping(name="getxxxx",
entities=@EntityResult(xxxx.class,
fields = {
@FieldResult(name="x1", column = "x1"),
@FieldResult(name="x2", column = "x2")}))
@NamedNativeQuery(name=" getxxxx ",
query="select x1, x2 from yyyy",
resultSetMapping=" getxxxx ")
} )public class xxxx{
.
.
.
public xxxx() {
}
我收到一个错误: “表”xxxx“无法解析”,类xxxx不是映射到我的源的表, 我正在尝试查询数据库并将结果返回到我的班级 有可能吗?
答案 0 :(得分:0)
在这种情况下,我要尝试的第一件事就是删除@Entity注释。然后更改类名或本机查询名,以便其中一个是“xxxx”,其中一个是“zzzz”,这样我就确定我知道运行时抱怨的是什么。
答案 1 :(得分:0)
听起来 xxxx 不应该是实体bean,因为JPA不满意在非实体bean中返回结果。您只需使用SQL字符串调用 createNativeQuery 。然后调用 query.getResultList()以 List(Object [])的形式获取结果,并使用它来填充非实体结果bean。
几年前我写了blog post,这可能会帮助您使用JPA执行高级本机查询。
答案 2 :(得分:0)
是的,这是可能的,但有点棘手。这是一个复杂的例子,应涵盖大部分基础。在这个例子中:
这是架构:
CREATE TABLE "public"."invoice" (
id SERIAL,
company_id int,
due_date date,
PRIMARY KEY(id)
);
CREATE TABLE "public"."item" (
id SERIAL,
invoice_id int,
description text,
PRIMARY KEY(id)
);
CREATE TABLE "public"."company" (
id SERIAL,
name text,
PRIMARY KEY(id)
);
INVOICE对象(为了完整性而令人费解的例子):
@Entity
@Table(name = "invoice")
@Loader(namedQuery = "loadInvoiceObject")
@NamedNativeQuery(name="loadInvoiceObject",
query="SELECT " +
"inv.id," +
"inv.due_date," +
"co.*," +
"it.*," +
"FROM invoice inv " +
"JOIN company co ON co.id = inv.company_id " +
"LEFT OUTER JOIN item it ON it.invoice_id = inv.id " +
"WHERE inv.id = :id",
resultSetMapping = "invoicemap")
@SqlResultSetMapping(name = "invoicemap",
entities = {
@EntityResult(entityClass = Invoice.class),
@EntityResult(entityClass = Company.class),
@EntityResult(entityClass = Item.class)
}
)
public class Invoice {
private Integer id;
private Date dueDate;
private Company company;
private List<Item> items = new ArrayList<Item>();
public Invoice() { /* no-args constructor */ }
@Id
@Column(name = "id", nullable = false)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "due_date")
@Temporal(TemporalType.DATE)
public Date getDueDate() { return dueDate; }
public void setDueDate(Date dueDate) { this.dueDate = dueDate; }
@ManyToOne(optional = false)
@JoinColumn(name = "company_id", nullable = false)
public Company getCompany() { return company; }
public void setCompany(Company company) { this.company = company; }
@OneToMany(mappedBy = "invoice")
public List<Item> getItems() { return items; }
public void setItems(List<Item> items) { this.items = items; }
}
ITEM对象:
@Entity
@Table(name = "item")
public class Item {
private Integer id;
private String description;
private Invoice invoice;
public Item() { /* no-args constructor */ }
@Id
@Column(name = "id", nullable = false)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "description")
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
@ManyToOne(optional = false)
@JoinColumn(name = "invoice_id", nullable = false)
public Invoice getInvoice() { return invoice; }
public void setInvoice(Invoice invoice) { this.invoice = invoice; }
}
COMPANY对象:
@Entity
@Table(name = "company")
public class Company {
private Integer id;
private String name;
private List<Invoice> invoices = new ArrayList<Invoice>();
public Company() { /* no-args constructor */ }
@Id
@Column(name = "id", nullable = false)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "name")
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@OneToMany(mappedBy = "company")
public List<Invoice> getInvoices() { return invoices; }
public void setInvoices(List<Invoice> invoices) { this.invoices = invoices; }
}