This is a migrated thread and some comments may be shown as answers.

How to get rid of Hibernate and use database by JDBC-driver

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ILYA
Top achievements
Rank 1
ILYA asked on 18 Mar 2019, 08:37 AM

We need to get rid of Hibernate (change it to JDBC-driver).

We use springdemos and have already changed 90% of code, but we don't inderstand how to get rid of this function:

@Override
public DataSourceResult getList(DataSourceRequest request) {
    return request.toDataSourceResult(sessionFactory.getCurrentSession(), TObjectType.class);}

---

Full code of TObjectTypeDaoImpl (it's model):

--

package com.kendoui.spring.models;

import org.hibernate.Session;
import com.kendoui.spring.models.TObjectType;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Transactional
@Component
public class TObjectTypeDaoImpl implements TObjectTypeDao {
@Autowired
private SessionFactory sessionFactory;
private ConnectionToPostgresql cn;
{ try { cn = new ConnectionToPostgresql(); }
catch (Exception e) { e.printStackTrace(); } }

@SuppressWarnings("unchecked")
@Override
public List<TObjectType> getList() {
//return sessionFactory.getCurrentSession().createCriteria(TObjectType.class).list();
Connection connection = cn.getConnection();
List<TObjectType> result = new ArrayList<TObjectType>();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM \"TOBJECT_TYPE\"");
while (resultSet.next()) {
TObjectType to = new TObjectType();
to.setIdObjectType(resultSet.getString("ID_OBJECT_TYPE"));
to.setDescription(resultSet.getString("DESCRIPTION"));
to.setObject1C(resultSet.getString("OBJECT_1C"));
result.add(to);
}
return result;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

@Override
public DataSourceResult getList(DataSourceRequest request) {

return request.toDataSourceResult(sessionFactory.getCurrentSession(), TObjectType.class);

}

@Override
public void saveOrUpdate(List<TObjectType> products) {
// Session session = sessionFactory.getCurrentSession();
// for (TObjectType product : products) {
// session.saveOrUpdate(product);
// }
//Connection connection = cn.getConnection();
for (TObjectType product : products) {

Connection connection = cn.getConnection();
String item = product.getIdObjectType();
try {
if (item.equals(""))
{
System.out.println("New User");
Statement statement = connection.createStatement();
PreparedStatement preparedStmt = connection.prepareStatement("INSERT INTO \"TOBJECT_TYPE\" (\"DESCRIPTION\",\"OBJECT_1C\") VALUES (?, ?)");
preparedStmt.setString(1, product.getDescription());
preparedStmt.setString(2, product.getObject1C());
//preparedStmt.setString(1, product.getIdObjectType());
preparedStmt.executeUpdate();
connection.close();
}
else {
System.out.println("Update user");

PreparedStatement preparedStmt = connection.prepareStatement("UPDATE \"TOBJECT_TYPE\" SET \"DESCRIPTION\" = ?, \"OBJECT_1C\" = ? where \"ID_OBJECT_TYPE\" = ?");
preparedStmt.setString(1, product.getDescription());
preparedStmt.setString(2, product.getObject1C());
preparedStmt.setString(3, product.getIdObjectType());
preparedStmt.executeUpdate();
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();}
}
}

@Override //если добавляем
public void saveOrUpdate(TObjectType product) {
// Session session = sessionFactory.getCurrentSession();
// session.saveOrUpdate(product);
System.out.println("Вызвался метод saveOrUpdate(TObjectType product)");

}

@Override
public void delete(TObjectType product) {
System.out.println("Вызвался метод delete(TObjectType product)");
//Session session = sessionFactory.getCurrentSession();
//session.delete(session.load(TObjectType.class, product.getIdObjectType()));
}

@Override
public void delete(List<TObjectType> products) {
// Session session = sessionFactory.getCurrentSession();
// for (TObjectType product : products) {
// session.delete(session.load(TObjectType.class, product.getIdObjectType()));
// }
for (TObjectType product : products) {
Connection connection = cn.getConnection();
try {
PreparedStatement preparedStmt = connection.prepareStatement("DELETE FROM \"TOBJECT_TYPE\" WHERE \"ID_OBJECT_TYPE\" = ?");
preparedStmt.setString(1, product.getIdObjectType());
preparedStmt.executeUpdate();
connection.close();
}
catch (SQLException e){ e.printStackTrace();}
}
}
}

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 20 Mar 2019, 07:54 AM
Hi Ilya,

The ToDataSourceResult method applies the data expressions send from the Grid to the data collection passed to the method, so if different data should be used, you should only change the first parameter in the ToDataSourceResult with the new one:
return request.toDataSourceResult(**the new IEnumerable or IQueryable**, TObjectType.class);}


Regards,
Konstantin Dikov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
ILYA
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or