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
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); 
private String name; private String genre;private String name; private String genre;no annotations. The table is - jessezsessionFactory. More preciselyem. To understand how you got there. Perhaps the error is still there - Aleksey Shimanskyem, i.e.EntityManager- Aleksey Shimansky