将数据添加到数据库后,我尝试将这些数据发送到数据。 但JPA无法看到添加的数据。如果我重新部署,JPA可以看到该数据。 原因是什么以及如何解决?
实体
@Entity
@Table(name ="client")
@NamedQuery(name="Client.getAll", query = "SELECT c FROM Client c")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
@Column(name="first_name", length = 25)
private String firstName;
@Column(name="surname", length = 25)
private String surName;
@Column(name="password", length = 30)
private String password;
@Column(name="email")
private String email;
ClientDAO
public EntityManager em =Persistence.createEntityManagerFactory("Vlad").createEntityManager();
public void add(Client client){
em.getTransaction().begin();
em.persist(client);
em.getTransaction().commit();
}
public List<Client> findClientByEmail(String email){
return em.createQuery(
"SELECT c FROM Client c WHERE c.email LIKE :custEmail")
.setParameter("custEmail", email)
.setMaxResults(1)
.getResultList();
}
ClientService
public Client login(ClientDTO clientDTO){
List<Client> clientList;
clientList = clientDAO.findClientByEmail(clientDTO.getEmail());
validLogin(clientList);
Client client = clientList.get(0);
validLogin(clientDTO, client);
return client;
}
的persistence.xml
<persistence-unit name="Vlad" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/clients"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
UPD 1
**ClientService**
public void register(ClientDTO clientDTO){
validData(clientDTO);
uniqueEmail(clientDTO);
Client client=new Client();
client.setFirstName(clientDTO.getFirstName());
client.setSurName(clientDTO.getSurName());
client.setPassword(clientDTO.getPassword());
client.setEmail(clientDTO.getEmail());
client.setNickname(clientDTO.getNickname());
clientDAO.add(client);