I have run into a very strange error. I am setting up my backend to use JPA/Spring/Hibernate. I am able to connect to the database but whenever I access a table for the very first time, it deletes all columns and data and replaces it with what is in the entity.
I was just testing so I chose a few fields from the table and represented them in the entity object like so:
package xyz.data.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Users implements Serializable {
private static final long serialVersionUID = 4768962659322615643L;
@Id
@Column(name="UserID")
private String userID;
private String companyName;
private String userFName;
private String userName;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getUserFName() {
return userFName;
}
public void setUserFName(String userFName) {
this.userFName = userFName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
NOTE: I did not annotate the other fields with @Column as I was just testing. I have a resources file which Spring is reading:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:p="http://ift.tt/1jdM0fE"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/1cnl1uo
http://ift.tt/GArMu7
http://ift.tt/1ldEMZY
http://ift.tt/OGfeU2
http://ift.tt/KC395X
">
<tx:annotation-driven />
<!-- Activates scanning of @Autowired -->
<context:annotation-config />
<!-- Activates scanning of @Repository, @Component and @Service -->
<context:component-scan base-package="ca.xyz.data" />
</beans>
And then I do a find command like this:
package abc.service.user;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Component;
import ca.impactauto.data.domain.Sale;
import ca.impactauto.data.domain.Users;
@Component
public class UserService {
@PersistenceContext private EntityManager em;
public void find() {
//Users user = em.find(Users.class, "NNNC001118");
Users user = em.find(Users.class, "STATIC");
//return a column from finding STATIC. I realize now STATIC was
//not a primary key.
System.out.println("UserInfo found: "+user.getCompanyName());
}
}
Here "companyName" comes from the Users entity which is a column in the users table.
After the find command runs, it wipes out ALL existing data and ALL existing columns and replaces it with the fields in the entity but with no data. i.e. in this case there are 4 fields in the entity and after the find, these same 4 fields will appear in the table as they look like exactly in the entity.
This is a find command NOT a delete command of an entire table!! Any ideas?
Aucun commentaire:
Enregistrer un commentaire