相关疑难解决方法(0)

我是否需要persistence.xml中的<class>元素?

我有非常简单的persistance.xml文件:

<?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 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
        <class>pl.michalmech.eventractor.domain.User</class>
        <class>pl.michalmech.eventractor.domain.Address</class>
        <class>pl.michalmech.eventractor.domain.City</class>
        <class>pl.michalmech.eventractor.domain.Country</class>

        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>
Run Code Online (Sandbox Code Playgroud)

它的工作原理.

但是当我删除<class>元素时,应用程序看不到实体(所有类都带有注释@Entity).

是否有任何自动机制来扫描@Entity类?

java orm annotations hibernate jpa

106
推荐指数
7
解决办法
15万
查看次数

如何使用JPA/Hibernate自动注册实体:未知实体

我遇到了Hibernate/JPA配置问题,这会阻止我的JPA注释实体被自动注册:

java.lang.IllegalArgumentException: Unknown entity: com.example.crm.server.model.Language
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671)
    at com.example.crm.server.model.Language.persist(Language.java:64)
    at com.example.crm.server.LanguageTest.testPersistAndRemove(LanguageTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Run Code Online (Sandbox Code Playgroud)

在我的实体课中,我有:

@Entity
@Table(name="Languages")
public class Language implements Serializable
{
    @Id
    private Long id;
    private String name;
    // etc...
}
Run Code Online (Sandbox Code Playgroud)

在MySQL中,Languages表看起来像:

+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| Language_ID | int(11)  | NO   | PRI | NULL    |       | 
| Name        | char(18) | YES  |     | NULL    |       | 
+-------------+----------+------+-----+---------+-------+
2 rows in …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa

10
推荐指数
1
解决办法
4万
查看次数

类是受管理的,但未在persistence.xml文件中列出

我在我的项目中遇到以下异常:

管理类"com.testApp.domain.Register",但未在persistence.xml文件中列出

我的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="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.detection" value="class, hbm" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:." />
            <property name="hibernate.connection.username" value="SA" />
            <property name="hibernate.connection.password" value="" />

            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.timeout" value="300" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="3000" />

        </properties>
    </persistence-unit>

</persistence>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这篇 …

java spring persistence hibernate jpa

7
推荐指数
3
解决办法
3万
查看次数

标签 统计

hibernate ×3

java ×3

jpa ×3

annotations ×1

orm ×1

persistence ×1

spring ×1