I am working under a project that is update the data's in MySQL table using Hibernate. Whenever I run the project, the exception is shown as below.
[Batch update returned unexpected row count from update [0]; actual
row count: 0; expected: 1]
Controller
#RequestMapping(value = "/disableEmployeeMaster", method = RequestMethod.POST)
public #ResponseBody void disableEmployee(HttpServletRequest request)
{
EmployeeMaster employeeMaster = new EmployeeMaster();
try
{
String employeeId = request.getParameter("employeeId");
employeeMaster.setIsDel("Y");
mainService.disableEmployee(employeeId , employeeMaster);
}
catch(Exception e)
{
logger.error(e.getMessage());
}
}
Service Implementation
#Override
public void disableEmployee(String Id, EmployeeMaster employeeMaster) {
Session session = null;
Transaction transaction = null;
try
{
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.update(Id, employeeMaster);
transaction.commit();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
session.close();
}
}
You have't set employeeId to EmployeeMaster class object.
to update any entity needs it's primary key.
You can refer following code :
employeeMaster.setEmployeeId(employeeId);
Controller
#RequestMapping(value = "/disableEmployeeMaster", method = RequestMethod.POST)
public #ResponseBody void disableEmployee(HttpServletRequest request)
{
EmployeeMaster employeeMaster = new EmployeeMaster();
try
{
String employeeId = request.getParameter("employeeId");
employeeMaster.setEmployeeId(employeeId);
employeeMaster.setIsDel("Y");
mainService.disableEmployee(employeeId , employeeMaster);
}
catch(Exception e)
{
logger.error(e.getMessage());
}
}
Related
I'm trying to get my code to load a picture and save it in DB2. I'm not sure what to do next because the code below is failing. Can someone guide me on how to do this. I have generated an entity from the DB. Datatype for documentname(the actual file) is blob but when I generated the entity it appeared as byte[]. The method below are in the EJB.
#Entity
#Table(name = "DOCUMENTS", schema = "PORTAL")
public class Documents {
private int documentid;
private byte[] documentname;
#Id
#Column(name = "DOCUMENTID", nullable = false)
public int getDocumentid() {
return documentid;
}
public void setDocumentid(int documentid) {
this.documentid = documentid;
}
#Lob
#Basic(fetch = FetchType.LAZY)
#Column(name = "DOCUMENTNAME", nullable = true)
public byte[] getDocumentname() {
return documentname;
}
public void setDocumentname(byte[] documentname) {
this.documentname = documentname;
}
Here I'm trying to read or rather load the picture.
private byte[] readImage(String filename) {
byte[] imageData = null;
FileInputStream file = null;
try {
file = new FileInputStream(filename);
int size = file.available();
imageData = new byte[size];
file.read(imageData);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (file != null) file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return imageData;
}
This is basically where I think I'm loosing it.
public Boolean populateProfilePicture(int blobFileID, byte[] blobFIle) {
Connection con = null;
PreparedStatement pstmt = null;
int success = 0;
String empPhotoFile = "/home/mika/Pictures";
Documents fileTable = new Documents();
try {
con = dataSource.getConnection();
pstmt = con.prepareStatement("INSERT INTO Documents VALUES(?,?)");
pstmt.setInt(1, blobFileID);
pstmt.setBytes(2, readImage(empPhotoFile));
success = pstmt.executeUpdate();
} catch (SQLException e) {
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
if (success>=1)
{
return true;
}
return true;
}
byte[] is an appropriate type mapping for BLOB data, though you might want to look into your JPA implementation -- it may have details on how you can influence that mapping to be an InputStream etc. so you can use streaming APIs depending on the size of the files at hand.
That said, since you raw read into a byte array, it seems you might not actually care that much given how you've implemented readData().
Since you're using JPA, one might suggest that you just -- use JPA.
public byte[] readImage(Entity entity String filename) throws Exception {
byte[] imageData = null;
try ( FileInputStream file : file = new FileInputStream(filename) ){
int size = file.available();
imageData = new byte[size];
file.read(imageData);
return imageData;
} catch (IOException | FileNotFoundException e) {
throw e;
}
}
This just does the same thing, but it puts the data in the JPA entity instead of trying to mess with the connection. Should serialise to the database fine when your entity manager commits the unit of work.
EDIT: Here is a re-write of your populate* method as requested. Note that I'm manually managing the transaction, and making a lot of assumptions about your entitymanager. that stuff is a bigger/different question.
public void populateProfilePicture(int blobFileID, String employeePhoto) throws Exception {
// this is about you figuring out JPA.
EntityManager entityManager = getEntityManager()
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin()
try {
Documents documents = entityManager.find(Documents.class, blobFileID);
byte[] data readImage( employeePhoto );
documents.setDocumentname( data );
} catch(Exception e) {
transaction.setRollbackOnly();
throw e
}
} finally {
transaction.commit();
}
}
I have a Patients entity class which auto generates an id:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "personId", nullable = false, unique = true)
private Long personId;
public void copy (Patients patient) {
if (patient.getNationality() != null)
this.setNationality(patient.getNationality());
if (patient.getGivenName() != null)
this.setGivenName(patient.getGivenName());
if (patient.getMiddleName() != null)
this.setMiddleName(patient.getMiddleName());
if (patient.getPrefix() != null)
this.setPrefix(patient.getPrefix());
}
/**
* #return PERSONID
*/
public int getPersonId() {
return personId;
}
My addPerson in PersonDaoImpl :
public Patients addPerson(Patients person) {
Patients p = new Patients(person);
try {
em = factory.createEntityManager();
em.getTransaction().begin();
SimpleDateFormat sdfr = new SimpleDateFormat("yyyy-MM-
dd'T'HH:mm:ss.SSS+05:30");
Date date = new Date();
String dateCreated = sdfr.format(date);
p.setDateCreated(dateCreated);
em.persist(p);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
log.error("Exception caught :: " + e);
p = null;
}
em.close();
return p;
}
My update api in person service class:
#PUT
#Path("/person-manager-resource/updatePersonById")
#Produces("application/json")
#Consumes("application/json")
public Response update(Patients person) {
log.info("Inside UpdatePerson");
log.info(person.getPersonId());
dao = new PersonDaoImpl();
ObjectMapper mapper = new ObjectMapper();
person1 = dao.updatePerson(person);
String result = "";
try {
result = mapper.writeValueAsString(person1);
log.info("Person updated :: " + result);
} catch (JsonProcessingException e) {
log.info("Exception Caught :: " + e);
}
if (person1 != null) {
return Response.
status(Response.Status.OK.getStatusCode()).
entity(result).
build();
} else {
return Response.
status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).
entity(result).
build();
}
}
UpdatePerson:
public Patients updatePerson(Patients updatedPatient) {
Patients dbPatient = new Patients();
TypedQuery<Patients> query = null;
ObjectMapper mapper = new ObjectMapper();
try {
em = factory.createEntityManager();
String identifier = updatedPatient.getPersonIdentifiers().getIdentifier();
String queryStr = "SELECT c FROM Patients c where c.personIdentifiers.identifier = '" + identifier + "'";
query = em.createQuery(queryStr, Patients.class);
dbPatient = query.getSingleResult();
dbPatient.copy(updatedPatient);
em.getTransaction().begin();
em.merge(dbPatient);
em.getTransaction().commit();
} catch (Exception e) {
log.error("Exception caught :: " + e);
em.getTransaction().rollback();
dbPatient = null;
}
em.close();
return dbPatient;
}
I pass a json object through my REST api to create a patient entry:
{
"personId": 5,
"prefix": null,
"givenName": "Pooja roy",
"middleName": null
}
Now this is going fine. I take the same object, which now contains the auto-generated personId, in an api which is supposed to update the object. I pass the json in the Patients entity object. When I print this whole object, the personId is null.
Since it is null and primary key, I can't do a merge. I have to manually update the database object, which is a very lengthy process.
Any ideas why it is coming as null and how I can retrieve it?
I am using postgres.
I think the whole problem is caused by the implementation of the updatePerson method. You should implement the method as follows and it should work as expected, assuming the updatedPatient instance is a persistent entity (meaning it has an ID field set):
public Patients updatePerson(Patients updatedPatient) {
Patients mergedPatient = new Patients();
try {
em = factory.createEntityManager();
em.getTransaction().begin();
mergedPatient = em.merge(updatedPatient);
em.getTransaction().commit();
} catch (Exception e) {
log.error("Exception caught :: " + e);
em.getTransaction().rollback();
}
em.close();
return mergedPatient;
}
Now mergedPatient should contain the synchronized state.
Update:
alternative solution
For whatever reason you cannot use a setter for the ID field. Then the following might solve your problem:
public Patients updatePerson(Patients updatedPatient) {
Patients dbPatient = new Patients();
try {
em = factory.createEntityManager();
String identifier = updatedPatient.getPersonIdentifiers().getIdentifier();
em.getTransaction().begin();
dbPatient = em.find(Patients.class, Long.parseLong(identifier));
dbPatient.copy(updatedPatient);
em.getTransaction().commit();
} catch (Exception e) {
// ..:
dbPatient = null;
}
em.close();
return dbPatient;
}
As the em.find() method is executed inside of a transaction, the object returned is managed, which means any changes to that returned instance will be synchronized with the database when the transaction commits.
PersonId is an auto generated id. So, jpa doesn't allow for me to set a setter for personId. We only have getPersonId() method in the entity class.
So, in updatePerson(Patients person), when I am passing the person object, every setter is called and the object is thus created. Since, personId doesn't have a setter method, it is returned as null in that object.
I am trying to run a SELECT query using Hibernate Criteria API which is defined in the code below. I checked the console and it seems like the
query is running fine. Here is what I am getting in the console for the SQL Query :
Hibernate:
select
this_.VALUE_EMP_ID as y0_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
But just below the above SQL in the console, I see the error :
java.lang.ClassCastException: java.lang.Integer cannot be cast to abc.def.myproject.orm.EmployeeTopMetaData
at abc.def.myproject.orm.dao.impl.EmpDaoImpl.insertEmployeeDetails(EmployeeDaoImpl.java:50)
And Line #50 is the following line in the below method :
(EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class)
The following method is defined in EmployeeDaoImpl java class.
public boolean insertEmployeeDetails(Employee employee)
{
logger.debug("Starting EmployeeDaoImpl.insert() .....");
Session session = null;
Transaction tx = null;
boolean status = true;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
EmployeeTopMetaData empMetaData =
(EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class) // This is the line #50
.setProjection(Projections.property("valueEmpId"))
.add(Restrictions.eq("testingId", 1234))
.add(Restrictions.eq("company_employee_id", 3345))
.uniqueResult();
if (empMetaData == null || empMetaData. getvalueEmpId() < 1) { throw new Exception("Invalid empMetaData"); }
System.out.println("October 04 EmployeeTopMetaData: ");
System.out.println(empMetaData. getvalueEmpId());
// Some more code to go
session.persist(employee);
tx.commit();
} catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
status = false;
} finally {
session.close();
}
logger.debug("Completed EmployeeDaoImpl.insert() .....");
return status;
}
Here is my Entity Class EmployeeTopMetaData.java :
package abc.def.myproject.orm;
#Entity
#Table(name="EMPLOYEE_TOP_METADATA")
public class EmployeeTopMetaData
{
public int getTestingId() {
return testingId;
}
public void setTestingId(int testingId) {
this.testingId = testingId;
}
public int getCompanyEmpId() {
return company_employee_id;
}
public void setCompanyEmpId(int company_employee_id) {
this.company_employee_id = company_employee_id;
}
public int getvalueEmpId() {
return valueEmpId;
}
public void setvalueEmpId(int valueEmpId) {
this.valueEmpId = valueEmpId;
}
#Id
#Column(name="TESTING_ID")
private int testingId;
#Column(name="COMPANY_EMP_ID")
private int company_employee_id;
#Column(name="VALUE_EMP_ID")
private int valueEmpId;
}
Your query only returns "this_.VALUE_EMP_ID" an int value.
If you want to return a EmployeeTopMetaData, you have to change your query:
Hibernate:
select
this_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
But I suggest that if you just need VALUE_EMP_ID, it's better to change just the variable.
Integer empMetaData =
(Integer) session.createCriteria(EmployeeTopMetaData.class) // This is the line #50
.setProjection(Projections.property("valueEmpId"))
.add(Restrictions.eq("testingId", 1234))
.add(Restrictions.eq("company_employee_id", 3345))
.uniqueResult();
I have to write some dao tests for project where I want to:
create DDL schema from database (MySQL);
create tables in another test database in memory (H2);
insеrt some data to database;
select the just inserted item;
check some data from this item.
This is my test:
public class BridgeDBTest {
private static String JDBC_DRIVER;
private static String JDBC_URL;
private static String USER;
private static String PSWD;
private static final Logger logger = LoggerFactory.getLogger(BridgeDBTest.class);
#BeforeGroups(groups = "bridgeDB")
public void init(){
try {
JDBC_DRIVER = org.h2.Driver.class.getName();
JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
USER = "root";
PSWD = "";
new HibernateTestUtil().setDialect("org.hibernate.dialect.HSQLDialect")
.translateCreateDllToOutputStream(new FileOutputStream(new File("src/test/resources/createSchema.sql")));
RunScript.execute(JDBC_URL, USER, PSWD, "src/test/resources/createSchema.sql", Charset.forName("UTF8"), false);
insertDataset(readDataSet());
}
catch (Exception expt) {
expt.printStackTrace();
logger.error("!!!" + expt);
throw new RuntimeException(expt.getMessage());
}
}
#Test(groups = "bridgeDB")
public void getItem(){
BridgeDAOImpl dao = new BridgeDAOImpl();
dao.setSessionFactory(new HibernateTestUtil().getSessionFactory());
try {
Bridge bridge = dao.get(1L);
assert(bridge.getName().equals("TEST-CN-DEVBOX01"));
} catch (ServiceException e) {
e.printStackTrace();
}
}
#AfterGroups(groups = "bridgeDB")
public void dropTables(){
try {
new HibernateTestUtil().setDialect("org.hibernate.dialect.HSQLDialect")
.translateDropDllToOutputStream(new FileOutputStream(new File("src/test/resources/dropSchema.sql")));
}
catch (Exception expt) {
expt.printStackTrace();
logger.error("!!!" + expt);
throw new RuntimeException(expt.getMessage());
}
}
private IDataSet readDataSet() throws Exception{
return new FlatXmlDataSetBuilder().build(new File("src/test/resources/datasetForTest.xml"));
}
private void insertDataset(IDataSet dataSet) throws Exception{
IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PSWD);
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setDataSet(dataSet);
databaseTester.onSetup();
}
}
BridgeDAOImplused class HibernateUtilfrom src/main/..., but I need to use my class HibernateTestUtil from src/test/.... It's modified HibernateUtil fitted for my test (there I set parameters for Configuration class).
BridgeDAOImpl (See 5 line in try block):
public class BridgeDAOImpl extends GenericDAOImpl<Bridge, Long> implements BridgeDAO {
//...
public SearchResult<Bridge> list(int from, int limit, String filter, String order, Long authId) throws ServiceException {
SearchResult<Bridge> results = null;
Search search = new Search(Bridge.class);
Session session = getSessionFactory().getCurrentSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
search.setFirstResult(from);
search.setMaxResults(limit);
HibernateUtil.buildSearch(filter, order, search, aliases);
results = searchAndCount(search);
transaction.commit();
}
catch (Exception expt) {
logger.error("!!!", expt);
if (transaction != null) {
transaction.rollback();
}
throw new ServiceException(expt.getMessage());
}
finally {
// session.close();
}
return results;
}
//...
}
How I can test my dao without modifying it?
My DAO Class:
#SuppressWarnings("unchecked")
public int getRowCount(Map<String, Object> searchParam) throws DAOReadException {
List<Client> clientRow = null;
try {
Criteria criteria = Criteria.forClass(Client.class);
//set criteria search
for (String key : searchParam.keySet()) {
/*if(key.equals("ClientPK.clientId1")){
criteria.add(Restrictions.like("ClientPK.clientId", searchParam.get(key)));
}*/
if(key.equals("clientPK.clientId")){
criteria.add(Restrictions.eq(key, Integer.parseInt(searchParam.get(key).toString())));
}
if(key.equals("clientName")){
criteria.add(Restrictions.like(key, searchParam.get(key)));
}
if(key.equals("status")){
criteria.add(Restrictions.eq(key, Short.parseShort(searchParam.get(key).toString())));
}
//Bug# 12544 start
if(key.equals("orgId"))
{
criteria.add(Restrictions.eq("ClientPK.orgId", searchParam.get(key)));
}
//Bug# 12544 End
}
criteria.addOrder(Order.desc("createdDate"));
clientRow = (List<Client>) findByCriteria(criteria);
}
catch (Exception e) {
throw new DAOReadException(e);
}
int rowCount = 0;
if (clientRow != null) {
rowCount = clientRow.size();
}
return rowCount;
}
}
error is :
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: ClientPK of: com.vin.eretail.model.client.Client [select this from com.vin.eretail.model.client.Client as this where this.ClientPK.orgId=? order by this.createdDate desc]
seems to change like below:
//Bug# 12544 start
if(key.equals("orgId"))
{
criteria.add(Restrictions.eq("clientPK.orgId", searchParam.get(key)));
}
//Bug# 12544 End
still need to see your Client class