Hi I am using Mysql with Spring boot and when i am using the query method with the CrudRepository hibernate can't match with my table.
My User class :
@Entity
@Table(name="Utilisateur")
public class User{
@Id
private long id;
@Column(name="nom")
private String nom;
@Column(name="prenom")
private String prenom;
@Column(name="admin")
private boolean admin;
@Column(name="actif")
private boolean actif;
@Column(name="logins")
private String logins;
@Column(name="email")
private String email;
@Column(name="naissance")
private String naissance;
@Column(name="pwd")
private String pwd;
@Column(name="compte")
private String compte;
public User(){
}
public User(String nom, String prenom, boolean admin, boolean actif, String logins, String email, String naissance, String pwd, String compte) {
this.nom = nom;
this.prenom = prenom;
this.admin = admin;
this.actif = actif;
this.logins = logins;
this.email = email;
this.naissance = naissance;
this.pwd = pwd;
this.compte = compte;
}
/*Getters and setters*/
}
My second class implement CrudRepository :
@Repository
public interface UserDao extends CrudRepository<User,Long>{
public User findByEmail(String email);
public List<User> findByLogins(String logins);
public List<User> findAll();
@Query("SELECT u FROM Utilisateur u where id=?1")
public User findById(int Id);
}
And for finish i am using a class for test with junit :
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RecipyApplication.class)
@WebAppConfiguration
public class RecipyApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void should_get_user_by_id(){
User user = userDao.findById(1);
Assert.assertNotNull("the id can't be null ",user);
}
}
And i don't understand why hibernate can't match with my database when i am using @Query. Thank you
Instead of SELECT u FROM Utilisateur
use SELECT u FROM User
in the @Query.
We should use entity name rather than table name.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments