对如何使Hibernate正常运行感到困惑

时间:2012-03-30 06:24:35

标签: java hibernate netbeans

我在NetBeans中有以下项目: 1°A图书馆 2°服务器 3°一个客户端(这是一些MVC客户端,但没有View。) 4°一个具体的客户 5°一个“调试项目”,它运行1个服务器实例和2个具体客户端实例。

我正在努力使Hibernate工作,但我从网络和NetBeans获得了大量信息,我迷路了。 - 在Server项目中,我有这个由NetBeans wizzard生成的'META-INF / persistence.xml'文件:

        <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MasterMindServerPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:49237"/>
      <property name="javax.persistence.jdbc.password" value="password"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

但通常我运行“调试项目”,所以我知道通常NetBeans要求在正在运行的项目中复制所有配置文件。 (我实际上更习惯于Visual Studio。)在“调试项目”中,我有以下'/hibernate.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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost:49237;databaseName=MasterMind</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">password</property>
    <mapping class="h.User"/>
    <mapping class="h.Game"/>
    <mapping class="h.GameCodeColor"/>
    <mapping class="h.GameColor"/>
    <mapping class="h.GameTurn"/>
    <mapping class="h.GameTurnCodeColor"/>
  </session-factory>
</hibernate-configuration>

昨天一切都运行良好的h.User实体(我试图插入并选择数据)。 当我尝试将一些h.Game数据插入数据库时​​,我得到了一个“未知实体”异常。 然后我尝试将两个xml文件移动到服务器而根本不使用调试项目,我也尝试只保留其中一个。然后我把这些东西放回去,因为这是最好的配置,好吧......至少那个做了一点点的东西。 但是现在当我运行我的项目(“调试项目”或不同的服务器+客户端)时,服务器回复“未识别的异常”。我想如果我可以调试它,我会更精确的东西,我想原因是它无法连接到数据库。但是当我遇到这个问题时,没有断点在Server Project端工作,所以我甚至无法调试它。

所以我有两个问题: - 无法访问数据库或导致“未识别的异常”的任何内容。   - 当我得到这个我无法调试Project Server(没有断点工作) - h。游戏实体未知。 *

  • 那不是因为我会导入一些??? hibernate.Entity而不是一些??? persistence.Entity,因为我在互联网上阅读,因为我已经导入了??? persistence.Entity并检查了它至少两次。

感谢您的帮助。

编辑: 我在服务器代码中添加了一些“System.out.println(”...“),然后由于某种原因断点再次起作用,我已经能够获得更精确的执行消息: “无法在使用@IdClass注释的实体中找到属性(GameCodeColorGameId):h.GameCodeColor” 这是IdClass的代码:

package h;

import java.io.*;

public class GameCodeColorPK implements Serializable {
    protected int GameCodeColorGameId;
    protected int GameCodeColorIndex;

    public GameCodeColorPK() {}

    public GameCodeColorPK(int GameCodeColorGameId, int GameCodeColorIndex) {
        this.GameCodeColorGameId = GameCodeColorGameId;
        this.GameCodeColorIndex = GameCodeColorIndex;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final GameCodeColorPK other = (GameCodeColorPK) obj;
        if (this.GameCodeColorGameId != other.GameCodeColorGameId) {
            return false;
        }
        if (this.GameCodeColorIndex != other.GameCodeColorIndex) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 53 * hash + this.GameCodeColorGameId;
        hash = 53 * hash + this.GameCodeColorIndex;
        return hash;
    }
}

0 个答案:

没有答案
相关问题