Hibernate使用过程遇到的坑

来源:互联网 发布:模拟登陆知乎 编辑:程序博客网 时间:2024/06/10 03:52

版本5.1.0
使用Hibernate+JPA 注解
persistence.xml如下
注意:属性的name都是以hibernate开头

<?xml version="1.0" encoding="UTF-8"?><persistence version="1.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 persistence_1_0.xsd">    <persistence-unit name="entityManager">        <provider>org.hibernate.ejb.HibernatePersistence</provider>        <properties>            <property name="hibernate.archive.autodetection" value="class, hbm" />            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:33067/hibernate" />            <property name="hibernate.connection.username" value="root" />            <property name="hibernate.connection.password" value="root" />            <property name="hibernate.connection.provider_class"                value="org.hibernate.connection.C3P0ConnectionProvider" />            <property name="hibernate.c3p0.max_size" value="80" />            <property name="hibernate.c3p0.min_size" value="5" />            <property name="hibernate.c3p0.timeout" value="12000" />            <property name="hibernate.c3p0.max_statements" value="1000" />            <property name="hibernate.c3p0.idle_test_period" value="30000" />            <property name="show_sql" value="true" />            <property name="hibernate.hbm2ddl.auto" value="update" />        </properties>    </persistence-unit></persistence>

在使用注解的过程遇到的问题,name代表的名称
问题1、

@Entity@Table(name = "msg")

这种注解会报错 javax.persistence.Table.indexes()[Ljavax/persistence/Index;
可以这样写

@Entity(name = "msg")

问题2:
在使用@ManyToOne(cascade = CascadeType.ALL)注解是不能使用@JoinColumn(name = “next_id”),否则会报javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;异常。不加joincolnum自动生成外键id名称即可。

0 0