I study Hibernate and ran into a problem, according to the manual I did everything one by one, and all the same it was a mistake :(

hibernate.cfg.xml

<!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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_basics_tutorial</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">former</property> <mapping class="com.valeobet.server.arbitrage.Artist"/> </session-factory> </hibernate-configuration> 

Artist class (I will not insert getters and setters)

 @Entity public class Artist { @Id private int id; } 

Here is the method that causes the error

 public Artist createArtist(int id, String name, String genre) { Artist artist = new Artist(id, name, genre); em.persist(artist); //<----- ЭТА Š”Š¢Š ŠžŠšŠ --------- return artist; } 

Here is a mistake

 Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: com.valeobet.server.arbitrage.Artist at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:760) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:736) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:741) at com.valeobet.server.arbitrage.ArtistService.createArtist(ArtistService.java:17) at com.valeobet.server.arbitrage.HibernateBasicsTutorial.main(HibernateBasicsTutorial.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

Project structure

project structure java

UPD:

 public class ArtistService { private EntityManager em; public ArtistService(EntityManager em) { this.em = em; } public Artist createArtist(int id, String name, String genre) { Artist artist = new Artist(id, name, genre); em.persist(artist); return artist; } public void removeArtist(int id) { Artist artist = em.find(Artist.class, id); if (artist != null) { em.remove(artist); } } public Artist changeArtistGenre(int id, String genre) { Artist artist = em.find(Artist.class, id); if (artist != null) { artist.setGenre(genre); } return artist; } public Artist findArtist(int id) { return em.find(Artist.class, id); } public List<Artist> findAllArtists() { TypedQuery<Artist> query = em.createQuery("SELECT a FROM Artist a", Artist.class); return query.getResultList(); } 

UPD2:

 Configuration configuration = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession(); ArtistService service = new ArtistService(session); 
  • And in the class Artist, the name and genre as declared and with what annotations? Does this table already exist in the database? - RiÄ„ard BrugekÄ„aim
  • @ RiÄ„ardBrugekÄ„aim private String name; private String genre; private String name; private String genre; no annotations. The table is - jessez
  • Show more how to build sessionFactory . More precisely em . To understand how you got there. Perhaps the error is still there - Aleksey Shimansky
  • @ Alexey Shimansky added to the post - jessez
  • @jessez is not that. I'm interested in em , i.e. EntityManager - Aleksey Shimansky

2 answers 2

The problem is solved, google tutorials for 2015 and found a solution in the comments. Line (in UPD2):

 SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

was replaced by:

 SessionFactory sessionFactory = configuration.buildSessionFactory(); 

Thanks to all!

    private String name; private string genre; no annotations.

    That's the problem, the anotations should be to ALL columns of the table (and the corresponding class variables)

     @Entity @Table(name = "EMPLOYEE") public class Employee { @Id @GeneratedValue @Column(name = "id") private int id; 

    Here are a couple of less readable guides:

    • Are you sure? @Column(name = "first_name") required if the name of the column in the database and in the class are different. Is not it so? - Alexey Shimansky
    • Added annotations, the problem is not gone. - jessez