工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1820|回复: 18

hibernate 1-1有表连接

[复制链接]
发表于 2006-7-6 22:04 | 显示全部楼层 |阅读模式
在ORACLE下运行总是出错!
老是提示无该表或视图
可是稍微修改后在mysql中运行确成功了
不知道是何原因?
难道.....莫非.......吾通...???.....是数据库的问题?

既然mysql下没问题  那么除了3, Person.hbm.xml     4, Address.hbm.xml  外 其他应该都是没问题的啦?

oracle下的源码向导:
1,Person.java
2, Address.java
3, Person.hbm.xml
4, Address.hbm.xml
5, HibernateUtil.java
6,PersonManager.java
7, hibernate.cfg.xml
8, build.xml
 楼主| 发表于 2006-7-6 22:05 | 显示全部楼层

1,Person.java

package lee;

public class Person
{
        private int personid;
        private String name;
        private int age;

    private Address address;

        public void setPersonid(int personid) {
                this.personid = personid;
        }

        public void setName(String name) {
                this.name = name;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public int getPersonid() {
                return (this.personid);
        }

        public String getName() {
                return (this.name);
        }

        public int getAge() {
                return (this.age);
        }

    public Address getAddress(){
        return address;
    }

    public void setAddress(Address address){
        this.address = address;
    }       
}
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:06 | 显示全部楼层

2, Address.java

package lee;

public class Address
{
        private int addressid;
        private String addressdetail;
    private Person person;

        public Address(){
    }

    public Address(String addressdetail){
        this.addressdetail = addressdetail;
    }

        public void setAddressid(int addressid) {
                this.addressid = addressid;
        }

        public void setAddressdetail(String addressdetail) {
                this.addressdetail = addressdetail;
        }

        public int getAddressid() {
                return (this.addressid);
        }

        public String getAddressdetail() {
                return (this.addressdetail);
        }
   
    public Person getPerson(){
        return person;
    }

    public void setPerson(Person person){
        this.person = person;
    }
}
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:06 | 显示全部楼层

3, Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="lee">

    <class name="Person">
        <id name="personid" >
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="age"/>

        <join table="PersonAddress" optional="true">
            <key column="personId" unique="true"/>
            <many-to-one name="address"
                column="addressId"
                not-null="true"
                unique="true"/>
        </join>


    </class>


</hibernate-mapping>
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:07 | 显示全部楼层

4, Address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="lee">

    <class name="Address">
        <id name="addressid">
            <generator class="native"/>
        </id>
        <property name="addressdetail"/>

        <join table="PersonAddress" inverse="true" optional="true">
            <key column="addressId" unique="true"/>
            <many-to-one name="person"
                column="personId"
                not-null="true"
                unique="true"/>
        </join>
    </class>

</hibernate-mapping>
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:08 | 显示全部楼层

5, HibernateUtil.java

package lee;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil
{

    public static final SessionFactory sessionFactory;

    static
        {
        try
                {
            //采用默认的hibernate.cfg.xml来启动一个Configuration的实例
                        Configuration configuration=new Configuration().configure();
                        //由Configuration的实例来创建一个SessionFactory实例
            sessionFactory = configuration.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 final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException
        {
        Session s = (Session) session.get();
        //如果该线程还没有Session,则创建一个新的Session
        if (s == null)
                {
            s = sessionFactory.openSession();
            //将获得的Session变量存储在ThreadLocal变量session里
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:08 | 显示全部楼层

6,PersonManager.java

package lee;
import org.hibernate.Transaction;
import org.hibernate.Session;

import java.util.Date;
import java.util.Set;
import java.util.HashSet;

public class PersonManager
{

    public static void main(String[] args)
        {
        PersonManager mgr = new PersonManager();

        mgr.testPerson("My Event", new Date());

        HibernateUtil.sessionFactory.close();
    }

        private void testPerson(String title, Date theDate)
        {
                Session session = HibernateUtil.currentSession();
                Transaction tx = session.beginTransaction();
        Person p = new Person();
        p.setName("yeeku");
        p.setAge(28);
        Address a = new Address("广州====天河");
        session.persist(a);
        p.setAddress(a);
                //a.setPerson(p);
        session.persist(p);

                tx.commit();
                HibernateUtil.closeSession();
        }
   

}
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:09 | 显示全部楼层

7, hibernate.cfg.xml

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
        <!--连接数据库的Driver-->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <!--数据库连接url-->
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
        <!--用户名-->
        <property name="connection.username">scott</property>
        <!--密码-->
        <property name="connection.password">tiger</property>


     <property name="connection.pool_size">5</property>

        <!--使用的SQL对应的"方言",此处是Oracle9的"方言"-->
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

        <!--程序执行的时候是否显示真正的sql语句-->
        <property name="show_sql">true</property>
         <!-- Drop and re-create the database schema on startup -->
     <property name="hbm2ddl.auto">create</property>


        <mapping resource="Address.hbm.xml"/>
        <mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:10 | 显示全部楼层

8, build.xml

<?xml version="1.0"?>
<project name="hibernte" basedir="." default="">
    <path id="classpath">
        <fileset dir="..\..\lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="."/>
    </path>

        <target name="compile" description="Compile all source code">
        <javac destdir="." debug="true"
            deprecation="false" optimize="false" failonerror="true">
            <src path="."/>
            <classpath refid="classpath"/>
        </javac>
    </target>

        <target name="run" description="run the main class" depends="compile">
        <java classname="lee.PersonManager" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>


</project>
回复

使用道具 举报

 楼主| 发表于 2006-7-6 22:18 | 显示全部楼层

over 代码复制完毕!

这些什么一对一  一对多  多对多的  看的头都大了  
几N种情况         不知道wool    hjack等各位高手们能否发个贴来个总结呢?
哈哈

我还没看完这些     不过除了稍微模范一下外  基本不了解   特别是对于那些诸如 Person.hbm.xml之类的配置文件中关于一对多,多对多的,很多都看不懂 ,实在是太多太杂了。
要是有个总结性的贴就爽啦
虽然我知道  这样的贴要花费很多的精力才能总结和发布出来

总之一句话  期待着.....................
哈哈


如果诸位有空的话,再来一贴写个 hibernate中的表继承方面的 ,那就更好啦!哈哈   继续期待着..............

[ 本帖最后由 深圳情缘 于 2006-7-6 22:20 编辑 ]
回复

使用道具 举报

发表于 2006-7-7 00:22 | 显示全部楼层
楼上速度很快啊!
我也给那些 一对一  一对多  多对多的搞到头痛。
回复

使用道具 举报

 楼主| 发表于 2006-7-7 00:37 | 显示全部楼层

我是但求速度 不求质量的

现在是求速不求质
等主要的技术略读一遍了  再回头精读了
呵呵
没办法
就快要上班了
多而粗总比   精而少好
反正应届可以一边工作以边学的
回复

使用道具 举报

 楼主| 发表于 2006-7-7 00:39 | 显示全部楼层

powerwind 有何心得啊

我现在只是鹦鹉学舌     只会照搬    但还不怎么理解
回复

使用道具 举报

发表于 2006-8-8 19:31 | 显示全部楼层
认真看了下,发现楼主是用 Hibernate3 (而我学习的书是 Hibernate2 ),楼主用的是什么书呢?
最近开始看EJB(是EJB2.x),发现实体Bean和 Hibernate 的持久化类很相似,不过郁闷的是据说EJB3用了注解,和EJB2也很多不同。只好以不变应快变了。
回复

使用道具 举报

发表于 2006-8-8 20:38 | 显示全部楼层
楼上的,hibernate3比hibernate2多了不少东西,但通用的东西都没变.

EJB3是个让人期待的标准,但以后未必会成为通用的标准,整个业界都在观望.就目前来说,大多数大型的应用都是Hibernate+SLSB(Stateless Sessionbean),小型应用可能是spring+hibernate的方式.用EJB为的是分布式,还有把事务,性能等因素丢给容器去考虑,毕竟N十万一套的websphere可以比免费的spring+tomcat更好的处理这些东西.
回复

使用道具 举报

发表于 2006-8-8 20:40 | 显示全部楼层
楼主把异常贴出来,看看是什么错误.

还有,楼主在小功能没调通前先别做那么复杂,先看看各自没关联的时候能不能跑,如果能跑就先做单向关联,别一开始就做双向关联,这样不好调.
回复

使用道具 举报

发表于 2006-8-8 22:15 | 显示全部楼层
同意楼上.
回复

使用道具 举报

 楼主| 发表于 2006-8-13 11:00 | 显示全部楼层

[huffy][huffy]

原帖由 MJOfPowerwind 于 2006-8-8 19:31 发表
认真看了下,发现楼主是用 Hibernate3 (而我学习的书是 Hibernate2 ),楼主用的是什么书呢?
最近开始看EJB(是EJB2.x),发现实体Bean和 Hibernate 的持久化类很相似,不过郁闷的是据说EJB3用了注解,和EJB2也 ...


我不是看书的,我看的是别人给的powerpoint  ,现在准备看深入浅出Hibernate


晕哦  现在突然觉得Hibernate刚看完没多久,基本上忘的7788了

重新学习了  呵呵  唉  
怪不得很多人都说仅仅看是没用的!
回复

使用道具 举报

发表于 2006-8-13 11:05 | 显示全部楼层
原帖由 深圳情缘 于 2006-8-13 11:00 发表


我不是看书的,我看的是别人给的powerpoint  ,现在准备看深入浅出Hibernate


晕哦  现在突然觉得Hibernate刚看完没多久,基本上忘的7788了

重新学习了  呵呵  唉  
怪不得很多人都说仅仅看是没用的!


你的资料似乎很不错哦!
我也是在一边看一边忘,有点像张三丰传授太极剑给张无忌。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 16:58

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表