如何摆脱“java.lang.IllegalArgumentException:未知实体”在运行一个简单的休眠应用程序时?

发布于 2024-12-11 12:18:45 字数 5711 浏览 0 评论 0原文

我是休眠新手。使用它创建一个小应用程序时,我遇到以下异常:

线程“main”java.lang.IllegalArgumentException中的异常:未知实体:
model.org.hibernate.ejb.AbstractEntityManagerImpl.persist 的学生(AbstractEntityManagerImpl.java:223) 在controller.Main.main(Main.java:50)

有人可以帮我吗?

实体类如下:

Other details:
NetBeans Version: 6.7.1  
Hibernate : 3.2.5

实体学生

package model;  
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Students implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToOne(cascade=CascadeType.ALL)
    private Address address;

    public Address getAddress() {
        return address;
    }

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

    public String getName() {
        return name;
    }

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


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

另一个实体类

package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;  
@Entity

public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    private String city;


    private String zip;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

DAO 文件

package controller;  import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import model.Address;
import model.Students;
import org.hibernate.HibernateException;
public class Main {
    public static void main(String arr[])
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToOne2PU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tr= em.getTransaction();
        try{

            tr.begin();

            Address add1 = new Address();
            add1.setCity("pune");
            add1.setZip("09");
            Address add2 = new Address();
            add2.setCity("mumbai");
            add2.setZip("12");

            Students s1 = new Students();
            s1.setName("abc");
            s1.setAddress(add1);
            Students s2 = new Students();
            s2.setName("xyz");
            s2.setAddress(add2);

            em.persist(s1);
            em.persist(s2);

            tr.commit();
            emf.close();

        }
        catch(HibernateException e){
            e.printStackTrace();

        }
    }

}

persistence.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="OneToOnePU" transaction-type="JTA">    
    <provider>org.hibernate.ejb.HibernatePersistence</provider>    
    <jta-data-source>students</jta-data-source>    
    <exclude-unlisted-classes>false</exclude-unlisted-classes>    
    <properties>    
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>    
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>    
      <property name="hibernate.connection.username" value="app"/>    
      <property name="hibernate.connection.password" value="app"/>    
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>    
    </properties>    
  </persistence-unit>   
</persistence>

hibernate.cfg.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>  
  <session-factory>  
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>  
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>  
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>    
    <property name="hibernate.connection.username">app</property>  
    <property name="hibernate.connection.password">app</property>  
    <mapping class="model.Students"/>  
    <mapping class="model.Address"/>  
  </session-factory>  
</hibernate-configuration> 

I am new to Hibernate. While creating a small app using it I got following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity:
model.Students at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:223)
at controller.Main.main(Main.java:50)

Can anybody please help me out?

The Entity classes are as follows:

Other details:
NetBeans Version: 6.7.1  
Hibernate : 3.2.5

Entity Students

package model;  
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Students implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToOne(cascade=CascadeType.ALL)
    private Address address;

    public Address getAddress() {
        return address;
    }

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

    public String getName() {
        return name;
    }

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


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

Another Entity Class

package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;  
@Entity

public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    private String city;


    private String zip;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

The DAO file

package controller;  import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import model.Address;
import model.Students;
import org.hibernate.HibernateException;
public class Main {
    public static void main(String arr[])
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToOne2PU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tr= em.getTransaction();
        try{

            tr.begin();

            Address add1 = new Address();
            add1.setCity("pune");
            add1.setZip("09");
            Address add2 = new Address();
            add2.setCity("mumbai");
            add2.setZip("12");

            Students s1 = new Students();
            s1.setName("abc");
            s1.setAddress(add1);
            Students s2 = new Students();
            s2.setName("xyz");
            s2.setAddress(add2);

            em.persist(s1);
            em.persist(s2);

            tr.commit();
            emf.close();

        }
        catch(HibernateException e){
            e.printStackTrace();

        }
    }

}

The persistence.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="OneToOnePU" transaction-type="JTA">    
    <provider>org.hibernate.ejb.HibernatePersistence</provider>    
    <jta-data-source>students</jta-data-source>    
    <exclude-unlisted-classes>false</exclude-unlisted-classes>    
    <properties>    
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>    
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>    
      <property name="hibernate.connection.username" value="app"/>    
      <property name="hibernate.connection.password" value="app"/>    
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>    
    </properties>    
  </persistence-unit>   
</persistence>

The hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>  
  <session-factory>  
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>  
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>  
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>    
    <property name="hibernate.connection.username">app</property>  
    <property name="hibernate.connection.password">app</property>  
    <mapping class="model.Students"/>  
    <mapping class="model.Address"/>  
  </session-factory>  
</hibernate-configuration> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情换悲伤 2024-12-18 12:18:45

取决于项目结构,但可能通过将以下内容直接添加到 persistence-unit 元素下的 persistence.xml 中。

<class>model.Students</class>
<class>model.Address</class>

完全像这样:

<?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="OneToOnePU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>students</jta-data-source>
    <class>model.Students</class>
    <class>model.Address</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property name="hibernate.connection.username" value="app"/>
      <property name="hibernate.connection.password" value="app"/>
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
    </properties>
  </persistence-unit>
</persistence>

顺便问一下,为什么要在 persistence.xml 和 hibernate.cfg.xml 中配置像 hibernate.dialect 这样的属性?

Depends bit about project structure, but likely by adding following to persistence.xml directly under persistence-unit element.

<class>model.Students</class>
<class>model.Address</class>

Exactly like this:

<?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="OneToOnePU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>students</jta-data-source>
    <class>model.Students</class>
    <class>model.Address</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property name="hibernate.connection.username" value="app"/>
      <property name="hibernate.connection.password" value="app"/>
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
    </properties>
  </persistence-unit>
</persistence>

By the way, why do you configure properties like hibernate.dialect in both persistence.xml and hibernate.cfg.xml?

美羊羊 2024-12-18 12:18:45

我正在尝试这个,它正在与我一起工作:

@EntityScan( basePackages = {"com.yourpkghere"} 添加到 Application 类。
要像这样:

@EntityScan( basePackages = {"com.yourpkghere"})
@SpringBootApplication

I'm Trying this and it's working with me :

Add @EntityScan( basePackages = {"com.yourpkghere"} to Application class.
To be like this :

@EntityScan( basePackages = {"com.yourpkghere"})
@SpringBootApplication
吻风 2024-12-18 12:18:45

如何将实体映射到数据库表?
为此,您可以尝试将 @Table(name="???") 注释与 @Entity 一起使用,
尽管 ???表示该实体在数据库中的表名称。

How do you map your entities to the db tables?
You can try using @Table(name="???") annotation with @Entity for this purpose,
while ??? indicates the table name in the database for this entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文