“How to fix ‘ProductName is in unnamed module of loader 'app'' error in JAVA”
https://github.com/Aliaksandr3by/demo
java.lang.ClassCastException: class com.example.demo.entity.ProductName cannot be cast to class com.example.demo.entity.ProductName (com.example.demo.entity.ProductName is in unnamed module of loader 'app'; com.example.demo.entity.ProductName is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @4be9a66c) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
public class HibernateUtil {
public void setUp(String hibernateCfgXml, IHibernateUtil iHibernateUtil) throws Exception {
// SessionFactory sessionFactory = new Configuration()
// .configure(hibernateCfgXml)
// .buildSessionFactory();
try (final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure(Objects.requireNonNull(hibernateCfgXml, "hibernate.cfg.xml"))
.build();
) {
try (SessionFactory sessionFactory = new MetadataSources(registry)
.buildMetadata()
.buildSessionFactory();
) {
iHibernateUtil.Func(sessionFactory);
} catch (Exception e) {
e.printStackTrace();
StandardServiceRegistryBuilder.destroy(registry);
throw e;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
/---
public GreetingController() {
try {
new HibernateUtil().setUp("hibernate.buhloasp.cfg.xml", (sessionFactory) -> {
try (Session session = sessionFactory.openSession()) {
List<ProductName> data = session
.createQuery("from ProductName")
.list();
data.forEach(e -> System.out.println(e.getName()));
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
/-----
package com.example.demo.entity;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.io.Serializable;
@ToString(of = {"id", "name"})
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Table(schema = "dbo", name = "PRODUCTS_NAMES")
@Entity
public class ProductName implements Serializable {
@Id
@Setter
@Getter
@Column(name = "ID_PRODUCT", unique = true)
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private int id;
@Setter
@Getter
@NonNull
@Column(name = "NAME_PRODUCT")
private String name;
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- hibernate.productttr43.cfg.xml -->
<!-- START - SQL Server Connection Properties -->
<!-- SQL Server - Database Specific Property Details - Need to Change according to
Database Vendor -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">
jdbc:sqlserver://localhost\SQLEXPRESS2017:49172;databaseName=buhloasp;integratedSecurity=false;encrypt=false;trustServerCertificate=false;
</property>
<property name="connection.username">guest</property>
<property name="connection.password">guest</property>
<!-- <property name="hibernate.default_schema">dbo</property>-->
<!-- -->
<property name="hibernate.show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">100</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.example.demo.entity.ProductName"/>
<!-- <property name="default_entity_mode">dynamic-map</property>-->
<!-- <mapping resource="entity/ProductName.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>

Aucun commentaire:
Enregistrer un commentaire