休眠连接问题

发布于 2024-11-09 03:06:01 字数 4726 浏览 0 评论 0原文

我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请你别敷衍 2024-11-16 03:06:01

日志已经表明映射文件 hibernate.hbm.xml 内的 由于以下错误而格式不正确:

元素类型“class”的内容是
不完整,必须匹配
“(元*,子选择?,缓存?,同步*,注释?,tuplizer*,(id |复合id),鉴别器?,自然id?,(版本|时间戳)?,(属性|多对-一对一|组件|动态组件|属性|a ny|map|set|list|bag|idbag|array|primitive-array),((join,subclass*)|joined-subclass*|union-subclass*),loader?,sql-插入?,sql-更新?,sql-删除?,过滤器*,结果集*,(查询|sql-查询)*)“

此错误消息意味着您违反了 的某些要求,例如:

  • 它只能包含括号内列出的子元素
  • * 符号表示子元素可以出现零次或多次。
  • 这 ?符号表示子元素可以出现零次或一次。
  • 没有任何 * 和 ? sign ,该子元素必须包含且仅包含一次(例如 id )

可以参考这个 了解如何阅读 DTD 元素语法。

the log already said that the <class> inside the mapping file hibernate.hbm.xml is not properly formatted because of the following error :

The content of element type "class" is
incomplete, it must match
"(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array),((join,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)"

This error message means that you violate some requirements of the <class> , for example:

  • It can only contain the child elements listed inside the parenthese
  • The * sign means that child element can occur zero or more times.
  • The ? sign means that child element can occur zero or one time.
  • Without any * and ? sign , that child element must be included and included only once (e.g. id )

You can refer to this to know how to read the DTD elements syntax.

阳光的暖冬 2024-11-16 03:06:01

您应该在 .xml 文件上使用 check xmlvalidate xml 因为此菜单可通过 netbeans 使用,只需右键单击 xml 并检查

you should use on .xml file check xml and validate xml as this menu is availabe with netbeans just right click on xml and check

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文