Home > OS >  java.lang.IllegalArgumentException: Unknown entity:
java.lang.IllegalArgumentException: Unknown entity:

Time:09-28

I'm learning JPA and Hibernate from Udemy. I might be making a silly mistake. None of the answers helped me so far.

This is my project structure:

enter image description here

Here's my code:

Main.java

package client;

import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.Guide;  // <--- an entity
import entity.Student;  // <--- another entity
...
public class CascadesClient {
    public static void main(String[] args) {
        
                Session session = HibernateUtil.getSessionFactory().openSession();
                Transaction txn = session.getTransaction();
                try {
                    txn.begin();
                    
                    //persisting a new Student (using CascadeType.PERSIST) along with its associated Guide
                    Guide guide = new Guide("2000IM10901", "Ian Lamb", 2000);
                    Student student = new Student("2014AL50456", "Amy Gill", guide);
                    session.persist(student);       
                    txn.commit();
                }   catch(Exception e) {
                        if(txn != null) { txn.rollback(); }
                        e.printStackTrace();
                }   finally {
                        if(session != null) { session.close(); }
                }
    
    }
}

Student.java

package entity;
import javax.persistence.*;

@Entity
public class Student {
  ...
}

Guide.java

package entity;    
import javax.persistence.*;

@Entity
public class Guide {
  ...
}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/tbdone</property>
        <property name="connection.username">root</property>
        <property name="connection.password">cosmonauts</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <!-- Pretty print the SQL in the log file and console -->
        <property name="format_sql">true</property>
        
        <!-- Create/update tables automatically using mapping metadata -->
        <property name="hbm2ddl.auto">update</property>
        
        <mapping class="entity.Guide" />    
        <mapping class="entity.Student" />
        
    </session-factory>
</hibernate-configuration>

pom dependencies

  <dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.5.6.Final</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>

Why I'm getting this exception:

java.lang.IllegalArgumentException: Unknown entity: entity.Student at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:752) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:735) at client.CascadesClient.main(CascadesClient.java:23)

CodePudding user response:

I figured out that dialect was the issue.

Below changes in hibernate.cfg.xml solved my issue:

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

CodePudding user response:

Can you please try to add the following to your xml:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
    <list>
        <value>entity</value> 
    </list>
</property>
<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
    </props>
</property>
</bean>

  
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
</bean>
  • Related