休眠连接问题
我已经为 NetBeans 设置了 hibernate 插件,但无法连接到 MySQL db - 代码:
package client;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
public class HiberTest {
private static SessionFactory sessionFactory;
private int id;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
protected static void setUp() throws Exception {
// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
}
public static void main(String[] args) throws Exception {
setUp();
Session session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery("CREATE TABLE test(myInt int not null)");
List resultList = q.list();
System.out.println(resultList);
session.getTransaction().commit();
session.close();
}
}
HibernateUtils.class
package client;
import org.hibernate.cfg.*;
import org.hibernate.SessionFactory;
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
错误:
May 22, 2011 12:40:33 PM org.hibernate.hql.ast.ErrorCounter reportError
SEVERE: line 1:1: unexpected token: CREATE
Exception in thread "main" java.lang.IllegalArgumentException: node to traverse cannot be null!
at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at client.HiberTest.main(HiberTest.java:36)
Java Result: 1
请帮助我解决这个问题,这对我处理 hibernate 来说是一个拖累。 在所有 cfg 更改之后,我决定发布 cfg.xml 和 hbm.xml 可能有人会在这里提出一些建议: cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://myhost:3306/wwwgeeksearthcom_geeksearth_test</property>
<property name="hibernate.connection.username">user_name</property>
<property name="hibernate.connection.password">**********</property>
<property name="hibernate.show_sql">true</property>
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTransactionFactory</property>-->
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
和 hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="HiberTest" table="guests">
<id name="id" column="g_id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
I had set up hibernate plugin to NetBeans and can not connect to MySQL dbs - the code:
package client;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
public class HiberTest {
private static SessionFactory sessionFactory;
private int id;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
protected static void setUp() throws Exception {
// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
}
public static void main(String[] args) throws Exception {
setUp();
Session session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery("CREATE TABLE test(myInt int not null)");
List resultList = q.list();
System.out.println(resultList);
session.getTransaction().commit();
session.close();
}
}
HibernateUtils.class
package client;
import org.hibernate.cfg.*;
import org.hibernate.SessionFactory;
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
The errors:
May 22, 2011 12:40:33 PM org.hibernate.hql.ast.ErrorCounter reportError
SEVERE: line 1:1: unexpected token: CREATE
Exception in thread "main" java.lang.IllegalArgumentException: node to traverse cannot be null!
at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at client.HiberTest.main(HiberTest.java:36)
Java Result: 1
Please help me with this that is a drag for me to deal with hibernate.
After all cfg changes I desided to post cfg.xml and hbm.xml may be someone would suggest something here they are:
cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://myhost:3306/wwwgeeksearthcom_geeksearth_test</property>
<property name="hibernate.connection.username">user_name</property>
<property name="hibernate.connection.password">**********</property>
<property name="hibernate.show_sql">true</property>
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTransactionFactory</property>-->
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
and hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="HiberTest" table="guests">
<id name="id" column="g_id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
日志已经表明映射文件
hibernate.hbm.xml
内的
由于以下错误而格式不正确:此错误消息意味着您违反了
的某些要求,例如:可以参考这个 了解如何阅读 DTD 元素语法。
the log already said that the
<class>
inside the mapping filehibernate.hbm.xml
is not properly formatted because of the following error :This error message means that you violate some requirements of the
<class>
, for example:You can refer to this to know how to read the DTD elements syntax.
您应该在 .xml 文件上使用
check xml
和validate xml
因为此菜单可通过 netbeans 使用,只需右键单击 xml 并检查you should use on .xml file
check xml
andvalidate xml
as this menu is availabe with netbeans just right click on xml and check