Не могу разобраться почему падает ошибка Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: java.lang.Long
при запуске метода delete(Long id)
. Методы save(Item item), update(Item item) and findById(Long id
отрабатывают правильно. Подскажите в чём может быть ошибка.
Класс Item
:
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "ITEM")
public class Item {
private Long id;
private String name;
private Date dateCreated;
private Date lastUpdateDate;
private String description;
public Item() {
}
public Item(String name, Date dateCreated, Date lastUpdateDate, String description) {
this.name = name;
this.dateCreated = dateCreated;
this.lastUpdateDate = lastUpdateDate;
this.description = description;
}
@Id
@SequenceGenerator(name = "IT_SQ", sequenceName = "ITEM_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IT_SQ")
public Long getId() {
return id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
@Column(name = "DATE_CREATED")
public Date getDateCreated() {
return dateCreated;
}
@Column(name = "LAST_UPDATE_DATE")
public Date getLastUpdateDate() {
return lastUpdateDate;
}
@Column(name = "DESCRIPTION")
public String getDescription() {
return description;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public void setLastUpdateDate(Date lastUpdateDate) {
this.lastUpdateDate = lastUpdateDate;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", name='" + name + '\'' +
", dateCreated=" + dateCreated +
", lastUpdateDate=" + lastUpdateDate +
", description='" + description + '\'' +
'}';
}
}
Class ItemDAO
:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
public class ItemDAO {
private static SessionFactory sessionFactory;
private static final String SQL_GET_ITEM_BY_ID = "SELECT * FROM ITEM WHERE ID = ?";
public Item save(Item item){
Transaction transaction = null;
try(Session session = createSessionFactory().openSession()) {
transaction = session.getTransaction();
transaction.begin();
session.save(item);
transaction.commit();
System.out.println("Saved successfully");
}catch (HibernateException e){
System.err.println("Saved is failed");
System.err.println(e.getMessage());
if (transaction != null)
transaction.rollback();
}
return item;
}
public void update(Item item){
Transaction transaction = null;
try (Session session = createSessionFactory().openSession()){
transaction = session.getTransaction();
transaction.begin();
session.update(item);
System.out.println("Update was successful");
transaction.commit();
}catch (HibernateException e){
System.err.println("Save is failed");
System.err.println(e.getMessage());
if (transaction != null)
transaction.rollback();
}
}
public void delete(Long id){
Transaction transaction = null;
try (Session session = createSessionFactory().openSession()){
transaction = session.getTransaction();
transaction.begin();
session.delete(id);
System.out.println("Recording deleted successfully");
transaction.commit();
}catch (HibernateException e){
System.err.println("Save is failed");
System.err.println(e.getMessage());
if (transaction != null)
transaction.rollback();
}
}
@SuppressWarnings("unchecked")
public Item findById(Long id){
Item item;
try (Session session = createSessionFactory().openSession()){
NativeQuery query = session.createNativeQuery(SQL_GET_ITEM_BY_ID);
item = (Item) query.addEntity(Item.class).setParameter(1, id).getSingleResult();
}catch (HibernateException e){
System.err.println("Save is failed");
System.err.println(e.getMessage());
throw e;
}
System.out.println("Save is done");
return item;
}
private static SessionFactory createSessionFactory(){
if (sessionFactory == null){
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
}
Pom file
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>crud-servlet</groupId>
<artifactId>crud-servlet</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.16.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
File 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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@gromcode-lesson.cjqbbseqr63c.eu-central-1.rds.amazonaws.com:1521:ORCL</property>
<property name="hibernate.connection.username">main</property>
<property name="hibernate.connection.password">ifgjrkzr</property>
<property name="show_sql">true</property>
<mapping class="Item"></mapping>
</session-factory>
<!--<open_tag params>value</open_tag>-->
</hibernate-configuration>
И собственно стек трэйс с ошибкой:
июн 26, 2018 7:35:33 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.16.Final}
июн 26, 2018 7:35:33 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
июн 26, 2018 7:35:33 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
июн 26, 2018 7:35:34 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
июн 26, 2018 7:35:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
июн 26, 2018 7:35:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@gromcode-lesson.cjqbbseqr63c.eu-central-1.rds.amazonaws.com:1521:ORCL]
июн 26, 2018 7:35:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=main, password=****}
июн 26, 2018 7:35:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
июн 26, 2018 7:35:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
июн 26, 2018 7:35:35 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: java.lang.Long
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:1000)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:929)
at ItemDAO.delete(ItemDAO.java:61)
at Demo.main(Demo.java:23)
Caused by: org.hibernate.MappingException: Unknown entity: java.lang.Long
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1635)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:82)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:56)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:993)
... 3 more
Process finished with exit code -1
Вроде всё правильно, но чего-то где-то не хватает...