by Guest » 25 Jan 2025, 11:25
Ich verstehe nicht, was das Problem ist. In einem normalen Projekt lädt die Entität einwandfrei, aber wenn ich ein Dateimodul-info.java hinzufüge, sehe ich den folgenden Fehler: < /p>
konnte Persister Org nicht instanziieren. Hibernate.Persisterin.Entity.singLetableStableEntityPersistin < /p>
< /blockquote>
Module-info.java sieht so aus: < /p>
Code: Select all
module crud.organization {
requires jakarta.persistence;
requires jakarta.validation;
requires org.hibernate.orm.core;
requires org.slf4j;
requires java.naming;}
< /code>
In der Klasse lade eine Entität: < /p>
public static void main(String[] args) {
OrgCategoryDao orgCategoryDao = new OrgCategoryDao();
Optional category = orgCategoryDao.getById(2);
if(category.isPresent())
System.err.println("present " + category.get());
else System.err.println("not present");}
< /code>
HiNRNATEUTIL.java:
public class HibernateUtil {
private final static Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
private static StandardServiceRegistry registry = null;
private static @NotNull Optional sessionFactory = Optional.empty();
private synchronized static @NotNull Optional buildSessionFactory() {
try {
if (registry != null) shutdown();
registry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
return Optional.ofNullable(metadata.getSessionFactoryBuilder().build());
} catch (Throwable ex) {
System.err.println(ex.getMessage());
shutdown();
}
return Optional.empty();
}
public synchronized static @NotNull Optional getSessionFactory() {
if (sessionFactory.isPresent()) return sessionFactory;
sessionFactory = buildSessionFactory();
return sessionFactory;
}
public synchronized static void shutdown() {
if(registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}}
< /code>
orgCategoryDao: < /p>
public class OrgCategoryDao {
public @NotNull Optional getById(int id) {
final Optional factory = HibernateUtil.getSessionFactory();
if(factory.isEmpty()) return Optional.empty();
Session session = null;
Transaction transaction = null;
try{
session = factory.get().openSession();
transaction = session.beginTransaction();
OrgCategory category = session.get(OrgCategory.class, id);
transaction.commit();
return Optional.ofNullable(category);
}catch (Exception e){
if(transaction != null){
transaction.rollback();
}
logger.error(e.getMessage());
}finally {
if(session != null) session.close();
}
return Optional.empty();
}
}
OrgCategory:
Code: Select all
@Entity
@Table(name = "organization_category")
@NamedQuery(name = "getAllWithSortByName", query = "select oc from OrgCategory oc order by name")
public class OrgCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_category")
private int id;
@Column(name = "name_category")
private String name;
@Column(name = "promt_for_text")
private String promtForText;
protected OrgCategory() {}
public OrgCategory(@NotNull String name,
@NotNull String promtForText) {
this.name = name;
this.promtForText = promtForText;
}
@Override
public String toString() {
return "OrganizationCategory{" +
"id=" + id +
", name='" + name + '\'' +
", promtForText='" + promtForText + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPromtForText() {
return promtForText;
}
public void setPromtForText(String promtForText) {
this.promtForText = promtForText;
}
}
Abhängigkeiten:
Code: Select all
jakarta.persistence
jakarta.persistence-api
3.1.0
org.hibernate
hibernate-core
6.5.2.Final
Ich möchte mit Modulen arbeiten, kann es aber nicht, ich verstehe nichts in den Ruhezustandsprotokollen
Ich verstehe nicht, was das Problem ist. In einem normalen Projekt lädt die Entität einwandfrei, aber wenn ich ein Dateimodul-info.java hinzufüge, sehe ich den folgenden Fehler: < /p>
konnte Persister Org nicht instanziieren. Hibernate.Persisterin.Entity.singLetableStableEntityPersistin < /p>
< /blockquote>
Module-info.java sieht so aus: < /p>
[code]module crud.organization {
requires jakarta.persistence;
requires jakarta.validation;
requires org.hibernate.orm.core;
requires org.slf4j;
requires java.naming;}
< /code>
In der Klasse lade eine Entität: < /p>
public static void main(String[] args) {
OrgCategoryDao orgCategoryDao = new OrgCategoryDao();
Optional category = orgCategoryDao.getById(2);
if(category.isPresent())
System.err.println("present " + category.get());
else System.err.println("not present");}
< /code>
HiNRNATEUTIL.java:
public class HibernateUtil {
private final static Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
private static StandardServiceRegistry registry = null;
private static @NotNull Optional sessionFactory = Optional.empty();
private synchronized static @NotNull Optional buildSessionFactory() {
try {
if (registry != null) shutdown();
registry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
return Optional.ofNullable(metadata.getSessionFactoryBuilder().build());
} catch (Throwable ex) {
System.err.println(ex.getMessage());
shutdown();
}
return Optional.empty();
}
public synchronized static @NotNull Optional getSessionFactory() {
if (sessionFactory.isPresent()) return sessionFactory;
sessionFactory = buildSessionFactory();
return sessionFactory;
}
public synchronized static void shutdown() {
if(registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}}
< /code>
orgCategoryDao: < /p>
public class OrgCategoryDao {
public @NotNull Optional getById(int id) {
final Optional factory = HibernateUtil.getSessionFactory();
if(factory.isEmpty()) return Optional.empty();
Session session = null;
Transaction transaction = null;
try{
session = factory.get().openSession();
transaction = session.beginTransaction();
OrgCategory category = session.get(OrgCategory.class, id);
transaction.commit();
return Optional.ofNullable(category);
}catch (Exception e){
if(transaction != null){
transaction.rollback();
}
logger.error(e.getMessage());
}finally {
if(session != null) session.close();
}
return Optional.empty();
}
}
[/code]
OrgCategory:
[code]@Entity
@Table(name = "organization_category")
@NamedQuery(name = "getAllWithSortByName", query = "select oc from OrgCategory oc order by name")
public class OrgCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_category")
private int id;
@Column(name = "name_category")
private String name;
@Column(name = "promt_for_text")
private String promtForText;
protected OrgCategory() {}
public OrgCategory(@NotNull String name,
@NotNull String promtForText) {
this.name = name;
this.promtForText = promtForText;
}
@Override
public String toString() {
return "OrganizationCategory{" +
"id=" + id +
", name='" + name + '\'' +
", promtForText='" + promtForText + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPromtForText() {
return promtForText;
}
public void setPromtForText(String promtForText) {
this.promtForText = promtForText;
}
}
[/code]
Abhängigkeiten:
[code]
jakarta.persistence
jakarta.persistence-api
3.1.0
org.hibernate
hibernate-core
6.5.2.Final
[/code]
Ich möchte mit Modulen arbeiten, kann es aber nicht, ich verstehe nichts in den Ruhezustandsprotokollen