Hibernate入门创建遇到的问题

1.Unable to create requested service(连接数据库出错)

(1).Hibernate.cfg.xml的数据源配置出错;

(2).Configuration configuration=new Configuration().configure();这里出错,我就是忘写了config()

2.Mapping (RESOURCE) not found : com/ljw/entory/Person.hbm.xml : origin(com/ljw/entory/Person.hbm.xml)(找不到Person.hbm.xml)

原因是:编译时不解析src/main/java中的xml文件

解决办法:在pom.xml中添加:

<build>

        <resources>

            <resource>

                <directory>src/main/java</directory>

                <includes>

                    <include>**/*.xml</include>

                </includes>

            </resource>

        </resources>

    </build>

3。The server time zone value ‘�й���׼ʱ��’,说什么时区值无法辨认或表示多个时区,什么鬼。

百度了一下让我添加serverTimezone=GMT

<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT</property>

不知道原因,反正是出来了。哦!

4.Caused by: org.hibernate.tool.schema.spi.SchemaManagementException:

Unable to execute schema management to JDBC target

[create table order (id integer not null, order_time datetime, shop_id integer not null auto_increment, customer_id integer not null, car_id integer not null, primary key (shop_id, id, customer_id, car_id))]

一个问题弄了半天,竟然是让我把order表改成orders就成了,我也是醉了。说是数据库的版本问题,order是系统文件,不让改,还是咋的。这个教训记住了。

5.Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.ljw.entory.Customer. Expected: class java.lang.Integer, got class java.lang.String

at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)

at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)

at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)

at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)

at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2696)

at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)


原因:查询方式默认是以主键查询,我的查询条件不是主键,所以报错。

6.Exception in thread "main" java.lang.StackOverflowError(栈溢出)

当使用懒加载后,如客户-订单,要查询订单信息,他会调用toString方法,

public StringtoString() {

             return "Customer{" +"id=" +id +", name='" +name +'\'' +", orders=" +orders +'}';

}

它会打印order,会使用order对象,使用order对象,又会调用toString方法

public StringtoString() {

         return "Order{" +"id=" +id +", name='" +name +'\'' +", customer=" +customer +'}';

}

会打印出customer,会使用customer对象,又会调用toString方法,进入一个死循环,导致栈溢出。

要解决:只需要把其中一个toString方法的order去掉,打断死循环即可。

Hibernate:

    select

        customer0_.id as id1_3_0_,

        customer0_.name as name2_3_0_

    from

        Customer customer0_

    where

        customer0_.id=?

Hibernate:

    select

        orders0_.cid as cid3_4_0_,

        orders0_.id as id1_4_0_,

        orders0_.id as id1_4_1_,

        orders0_.name as name2_4_1_,

        orders0_.cid as cid3_4_1_

    from

        Orders orders0_

    where

        orders0_.cid=?

Customer{id=1, name='毛晓彤', orders=[Order{id=1, name='订单1'}]}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。