论坛首页 Java企业应用论坛

Hibernate能支持MS SQL7.0吗?

浏览 9527 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-11-14  
最近开始研究Hibernate,想把它用在SQL7.0上,(因为公司的项目都是用的7.0),在网上下载了一些例子,然后就开始埋头苦干,开发平台就是Win2000+Tomcat5.0+SQL7.0了,开始用的是www.freetds.org的TwFreeTds_1.3.jar,根本就不行,不能写数据道数据库,不过它倒是能建立一个空表在那里,呵呵,然后又换了http://jtds.sourceforge.net/的jtds-0.5.1.jar,这一次能写数据进表了,但是只是一些最简单的存储,一要一对一或者以上就不行,最后又换了大名顶顶的JSQLCONNECT也是同样的错误,所以这里想请问一下各位,尤其是斑竹老大,是不是Hibernate不支持MS SQL7.0啊?
在Tomcat5.0中错误是这样的:
Query language substitutions: {}
Hibernate: select c.CourseId as x0_0_ from Courses c where (c.Name like ? )
Hibernate: SELECT Cours0_.CourseId as CourseId, Cours0_.Name as Name FROM Courses Cours0_ WHERE Cours0_.CourseId=?
Hibernate: SELECT Stude0_.StudentId as StudentId__, Stude1_.StudentId as StudentId0_, Stude1_.Name as Name0_, Stude1_.ClassesId as ClassesId0_, Class2_.ClassesId as ClassesId1_, Class2_.Name as Name1_, Addre3_.AddressId as AddressId2_, Addre3_.City as City2_, Addre3_.State as State2_, Addre3_.Zip as Zip2_, Addre3_.Street as Street2_ FROM Student_Course_Link Stude0_ left outer join Students Stude1_ on Stude0_.StudentId=Stude1_.StudentId left outer join Classes Class2_ on Stude1_.ClassesId=Class2_.ClassesId left outer join Address Addre3_ on Stude1_.StudentId=Addre3_.AddressId WHERE Stude0_.CourseId=?
2003-11-14 12:25:58 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 208, SQLState: S0002
2003-11-14 12:25:58 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: Invalid object name 'Student_Course_Link'.
2003-11-14 12:25:58 net.sf.hibernate.JDBCException <init>
严重: SQLException initializing collection
com.jnetdirect.jsql.v: Invalid object name 'Student_Course_Link'.
at com.jnetdirect.jsql.v.a(Unknown Source)
at com.jnetdirect.jsql.at.a(Unknown Source)
at com.jnetdirect.jsql.af.i(Unknown Source)
at com.jnetdirect.jsql.af.new(Unknown Source)
at com.jnetdirect.jsql.af.do(Unknown Source)
at com.jnetdirect.jsql.ao.executeQuery(Unknown Source)
还有我看了下Hibernate产生的SQL语言,好象在SQL7.0下不能用啊
drop table Address;
drop table Classes;
drop table Courses;
drop table Students ;
drop table Student_Course_Link;

create table Address (AddressId varchar(32) not null,City varchar(32),Street varchar(32),State varchar(32),Zip varchar(32),constraint pk_Address primary key (AddressId),constraint fk_Address  foreign key (StudentId) references Students(StudentId));

create table Classes(ClassesId varchar(32) not null,name varchar(32),constraint pk_Classes primary key (ClassesId));

create table Courses(CourseId varchar(32) not null,name varchar(32),constraint pk_Courses primary key (CourseId));

create table Students (StudentId varchar(32) not null,name varchar(32),ClassesId varchar(32) not null,constraint pk_Students  primary key (StudentId),constraint fk_Students  foreign key (ClassesId) references Classes(ClassesId));

create table Student_Course_Link(CourseId varchar(32) not null,StudentId varchar(32) not null,constraint pk_Student_Course_Link primary key (CourseId,StudentId),constraint fk_Student_Course_Link_1 foreign key (CourseId) references Courses(CourseId),constraint fk_Student_Course_Link_2 foreign key (StudentId) references Students(StudentId)) ;
运行的结果是:
Server: Msg 1769, Level 16, State 1, Line 7
Foreign key 'fk_Address' references invalid column 'StudentId' in referencing table 'Address'.
Server: Msg 1750, Level 16, State 1, Line 7
Could not create constraint. See previous errors.
不知道我这么写大家看的明白吗?呵呵!
初次来到这个论坛,希望大家能做朋友,还望各位大大不吝赐教哦!
   发表时间:2003-11-14  
想想看还是贴代码把,免得被斑竹骂,呵呵,贴一个查询的,就是上面抱错的哪一个。
<%@ page import="java.sql.*,java.util.*,com.hellking.study.hibernate.Course" errorPage="error.jsp"%>
<jsp:useBean id="courseBusiness" class="com.hellking.study.hibernate.CourseBean" scope="page"/>
<html><body><center>
以下是名字中包含<%=request.getParameter("name")%>的course:<br>
<table border=1><tr><td>id</td><td>name</td></tr>
<% try
{
   Iterator it=courseBusiness.getSomeCourse((String)request.getParameter("name"));
   while(it.hasNext())
   {
     Course temp=(Course)it.next();
     out.println("<tr><td>"+temp.getId()+"</td>");
     out.println("<td>"+temp.getName()+"</td></tr>");
   }
  }
  catch(Exception e)
  {
    out.println(e.getMessage());
   }
%>
</table>  
  <a href="course.jsp"><<<--</a>

</center>
</body>
</html>
。。。。。。。。。。。。。。
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/hibernate</property>
        <property name="show_sql">true</property>
        <property name="dialect">net.sf.hibernate.dialect.SybaseDialect</property>

        <!-- Mapping files -->
        <mapping resource="Address.hbm.xml"/>
        <mapping resource="Student.hbm.xml"/>
        <mapping resource="Classes.hbm.xml"/>
        <mapping resource="Course.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
。。。。。。。。。。。。。。。。。。。。
web.xml片断:

<resource-ref>
  <description>SqlServer Datasource example</description>
  <res-ref-name>jdbc/hibernate</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>
。。。。。。。。。。。。。。。。。。。。
server.xml片断:
<Context path="/hibernate" docBase="hibernate" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/hibernate" auth="Container" type="javax.sql.DataSource"/>  
     <ResourceParams name="jdbc/hibernate">
     <parameter>
         <name>factory</name>
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
     </parameter>
     <parameter>
        <name>driverClassName</name>
          <value>com.jnetdirect.jsql.JSQLDriver</value>
     </parameter>
       <parameter>
           <name>url</name>
           <value>jdbc:JSQLConnect://localhost:1433/wtq</value>  
         </parameter>
     <parameter>
         <name>username</name>
         <value>wtq</value>
     </parameter>
     <parameter>
        <name>password</name>
        <value>password</value>
          </parameter>
     <parameter>
    <name>maxActive</name>
    <value>20</value>
  </parameter>
         <parameter>
     <name>maxIdle</name>
          <value>10</value>
  </parameter>
  <parameter>
         <name>maxWait</name>
    <value>-1</value>
  </parameter>
    </ResourceParams>  
  </Context>
差不多了吧,呵呵。
0 请登录后投票
   发表时间:2003-11-14  
引用

create table Address (AddressId varchar(32) not null,City varchar(32),Street varchar(32),State varchar(32),Zip varchar(32),constraint pk_Address primary key (AddressId),constraint fk_Address foreign key (StudentId) references Students(StudentId));

Address table没有StudentId列呀?
0 请登录后投票
   发表时间:2003-11-14  
这个SQL语言好像是自己生成的,难道是我用的例子中的映射关系没对?
我对or映射才开始学习。不知道它错在哪里?
能帮忙看下吗?
0 请登录后投票
   发表时间:2003-11-14  
那把映射贴出。
0 请登录后投票
   发表时间:2003-11-14  
Address.hbm.xml
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.hellking.study.hibernate.Address"
        table="Address"
        dynamic-update="false"
    >

        <id
            name="id"
            column="AddressId"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned"/>
        </id>

        <property
            name="city"
            type="string"
            update="false"
            insert="true"
            column="City"
        />

        <property
            name="state"
            type="string"
            update="true"
            insert="true"
            column="State"
        />

        <property
            name="zip"
            type="string"
            update="true"
            insert="true"
            column="Zip"
        />
      
        <property
            name="street"
            type="string"
            update="true"
            insert="true"
            column="Street"
        />
     
    </class>

</hibernate-mapping>
................................
Classes.hbm.xml
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.hellking.study.hibernate.Classes"
        table="Classes"
        dynamic-update="false"
    >

        <id
            name="id"
            column="ClassesId"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned"/>
        </id>

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />
       
        <set
            name="students"
            table="Students"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="ClassesId"
              />

              <one-to-many
                  class="com.hellking.study.hibernate.Student"
              />
        </set>

     
    </class>

</hibernate-mapping>
....................................................
Course.hbm.xml
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.hellking.study.hibernate.Course"
        table="Courses"
        dynamic-update="false"
    >

        <id
            name="id"
            column="CourseId"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned"/>
        </id>

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />
       
        <set
            name="students"
            table="Student_Course_Link"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="CourseId"
              />

              <many-to-many
                  class="com.hellking.study.hibernate.Student"
                  column="StudentId"
                  outer-join="auto"
              />

        </set>

     
    </class>

</hibernate-mapping>
........................................
Student.hbm.xml
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.hellking.study.hibernate.Course"
        table="Courses"
        dynamic-update="false"
    >

        <id
            name="id"
            column="CourseId"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned"/>
        </id>

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />
       
        <set
            name="students"
            table="Student_Course_Link"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="CourseId"
              />

              <many-to-many
                  class="com.hellking.study.hibernate.Student"
                  column="StudentId"
                  outer-join="auto"
              />

        </set>

     
    </class>

</hibernate-mapping>
0 请登录后投票
   发表时间:2003-11-14  
不好意思,是不是贴的太乱了,大家看着头晕啊!?
呵呵,都没人回答我。
我也搞不懂为什么会有这样的sql语言:
create table Address (AddressId varchar(32) not null,City varchar(32),Street varchar(32),State varchar(32),Zip varchar(32),constraint pk_Address primary key (AddressId),constraint fk_Address foreign key (StudentId) references Students(StudentId));
我也觉得是错误的,但是这是例子里面自己带的阿。
作者把它放在里面是什么意思了?
还有通过hibernate生成的sql语言应该都是一样的吧?和用什么驱动没有关系,我的理解对吗?
后面我又重装了个SQL2000.错误还是一样,现在真的头大,大家帮帮忙,好吗?
在这里先谢谢了!!!
0 请登录后投票
   发表时间:2003-11-14  
自己顶一顶,摸石头过河,已经折腾了几天了。
大家给我点光明吧!  
0 请登录后投票
   发表时间:2003-11-15  
最好用code格式化一下,看的头晕
0 请登录后投票
   发表时间:2003-11-15  
不知道怎么用code,所以我想把整个应用打成包上传上来,但是试了几次都不成功,斑竹你能给我你的信箱吗?
我发给你看看,好吗?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics