我爲我的最終項目使用hibernate。我有一張名爲designs
的表格,用於存儲藝術品的所有細節。在探索頁面中,我需要使用hibernate
從數據庫中獲取所有設計。所以,我創建了一個名爲designModal.java
的Java類,無法解決休眠錯誤QuerySyntaxException:設計未映射[來自設計]
public class designModal {
SessionFactory sf;
public designModal() {
sf = connection.NewHibernateUtil.getSessionFactory();
}
public String[] getAllDesigns() {
String result[] = new String[8];
try {
Session ses = sf.openSession();
Transaction tr = ses.beginTransaction();
List<Designs> designList = ses.createQuery("from designs").list();
for (Designs designs : designList) {
result[0]= designs.getDesignId().toString();
result[1]= designs.getTitle();
result[2]= designs.getImage();
result[3]= designs.getDescription();
result[4]= designs.getPrice().toString();
result[5]= designs.getViews().toString();
result[6]= designs.getLikes().toString();
result[7]= designs.getDesigners().toString();
}
tr.commit();
ses.flush();
ses.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result[0]+""+result[1]+""+result[3]);
return result;
}
}
然後,我在我的頁面調用此方法,
<%@page import="modal.designModal"%>
<%
String result[] = new designModal().getAllDesigns();
out.print(result.length);
%>
,但我得到一個例外:QuerySyntaxException: designs is not mapped [from designs]
我跟一些所謂的問題,但該沒有幫助。所以我創建了一個新的Web項目,但仍未解決。這裏有什麼問題?謝謝
Design.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 11, 2016 1:11:09 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="pojos.Designs" table="designs" catalog="design">
<id name="designId" type="java.lang.Integer">
<column name="design_id" />
<generator class="identity" />
</id>
<many-to-one name="designers" class="pojos.Designers" fetch="select">
<column name="designer_id" not-null="true" />
</many-to-one>
<many-to-one name="galleries" class="pojos.Galleries" fetch="select">
<column name="gallery_id" not-null="true" />
</many-to-one>
<property name="title" type="string">
<column name="title" length="45" />
</property>
<property name="image" type="string">
<column name="image" length="100" />
</property>
<property name="description" type="string">
<column name="description" length="500" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0" />
</property>
<property name="views" type="java.lang.Integer">
<column name="views" />
</property>
<property name="likes" type="java.lang.Integer">
<column name="likes" />
</property>
<set name="buysRegisters" table="buys_register" inverse="true" lazy="true" fetch="select">
<key>
<column name="design_id" not-null="true" />
</key>
<one-to-many class="pojos.BuysRegister" />
</set>
</class>
</hibernate-mapping>
Design.java
package pojos;
// Generated Oct 11, 2016 1:11:09 PM by Hibernate Tools 3.6.0
import java.util.HashSet;
import java.util.Set;
/**
* Designs generated by hbm2java
*/
public class Designs implements java.io.Serializable {
private Integer designId;
private Designers designers;
private Galleries galleries;
private String title;
private String image;
private String description;
private Double price;
private Integer views;
private Integer likes;
private Set buysRegisters = new HashSet(0);
public Designs() {
}
public Designs(Designers designers, Galleries galleries) {
this.designers = designers;
this.galleries = galleries;
}
public Designs(Designers designers, Galleries galleries, String title, String image, String description, Double price, Integer views, Integer likes, Set buysRegisters) {
this.designers = designers;
this.galleries = galleries;
this.title = title;
this.image = image;
this.description = description;
this.price = price;
this.views = views;
this.likes = likes;
this.buysRegisters = buysRegisters;
}
public Integer getDesignId() {
return this.designId;
}
public void setDesignId(Integer designId) {
this.designId = designId;
}
public Designers getDesigners() {
return this.designers;
}
public void setDesigners(Designers designers) {
this.designers = designers;
}
public Galleries getGalleries() {
return this.galleries;
}
public void setGalleries(Galleries galleries) {
this.galleries = galleries;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getViews() {
return this.views;
}
public void setViews(Integer views) {
this.views = views;
}
public Integer getLikes() {
return this.likes;
}
public void setLikes(Integer likes) {
this.likes = likes;
}
public Set getBuysRegisters() {
return this.buysRegisters;
}
public void setBuysRegisters(Set buysRegisters) {
this.buysRegisters = buysRegisters;
}
}
HibernateUtil.java
package connection;
public class NewHibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
什麼是用於映射設計表中的Java類的名字嗎?顯然沒有名爲**設計的類已經映射。 –
我想你在問關於util類。我使用Netbeans休眠嚮導。 – Malinda
你可以更新你的問題,包括** Design **課程。 –