4
I am new to the world of Hibernate and when I run this application, I get the following exception:
java.lang.IllegalArgumentException: Unknown entity: com.nataniel.api.domain.User
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:840)
at com.nataniel.api.services.UserService.createUser(UserService.java:44)
User Entity:
package com.nataniel.api.domain;
import org.hibernate.annotations.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER")
public class User {
@Id
@GeneratedValue
private Long id;
@Column(name = "LOGIN")
private String login;
@Column(name = "NAME")
private String name;
@Column(name = "EMAIL")
private String email;
@Column(name = "PASSWORD")
private String password;
@Column(name = "CITY")
private String city;
@Column(name = "REGION")
private String region;
@Column(name = "BIRTHDATE")
private String birthDate;
public User() {
}
public User(String login, String name, String email, String password, String city, String region, String birthDate) {
this.login = login;
this.name = name;
this.email = email;
this.password = password;
this.city = city;
this.region = region;
this.birthDate = birthDate;
}
// getters and setters
}
DAO ARCHIVE:
package com.nataniel.api.services;
import org.apache.camel.Exchange;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import com.nataniel.api.domain.User;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
@Service("userService")
public class UserService {
@PersistenceContext
transient EntityManager entityManager;
@Transactional
public String createUser(Exchange exchange) {
JSONObject userAccountJSON = (JSONObject) exchange.getIn().getHeader("jsonRequest");
User user = new User();
user.setLogin(userAccountJSON.getString("login"));
user.setEmail(userAccountJSON.getString("email"));
user.setPassword(userAccountJSON.getString("password"));
user.setName(userAccountJSON.getString("name"));
user.setCity(userAccountJSON.getString("city"));
user.setRegion(userAccountJSON.getString("region"));
user.setBirthDate(userAccountJSON.getString("birthdate"));
EntityManagerFactory factory = Persistence.createEntityManagerFactory("service-provider");
entityManager = factory.createEntityManager();
entityManager.persist(user);
entityManager.close();
return userAccountJSON.toString();
}
}
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="service-provider">
<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- entidade mapeada -->
<class>com.nataniel.api.domain.User</class>
<properties>
<!-- dados da conexao -->
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://192.168.0.140:3306/service-provider" />
<property name="javax.persistence.jdbc.user"
value="root" />
<property name="javax.persistence.jdbc.password"
value="123" />
<!-- propriedades do hibernate -->
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- atualiza o banco, gera as tabelas se for preciso -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
Welcome to Stackoverflow in English, please translate your question.
– user28595
Could include the
import
s and the definition ofpackage
in the code of its classesUser
andUserService
?– Victor Stafusa
Because your method
createUser
creates aEntityManagerFactory
and destroys it instead of using theEntityManager
injected?– Victor Stafusa
@diegofm translated question!
– Nataniel Carvalho
@Victorstafusa I used in parts the material of this link: (https://www.caelum.com.br/apostila-java-web/uma-introducao-pratica-ao-jpa-com-hibernate/#14-10-recording-recording-and-gathering-objects). Also, when using the injected Entitymanager gives Nullpointerexception in the Userservice persist.
– Nataniel Carvalho
@Natanielcarvalho If the
EntityManager
injected givesNullPointerException
, means there was no injection. In this case, either you abandon the dependency injection at once and do it all by hand, or you fix the dependency injection and do nothing at hand. What you can’t keep is a Frankenstein that starts from one thing and starts from another.– Victor Stafusa
Anyway, your question seems to be pertinent and it doesn’t seem like a silly mistake, so take my +1 vote. I don’t know yet what the solution is.
– Victor Stafusa