Why is this unit test failing when I run it in Debug mode using a break-point at the line
while (rs.next()) {.
It will exit the loop after the first iteration despite the instruction in the test.
Mockito.when(mockResultSet.next()).thenReturn(true, true, false);
this unit test works fine when running it in normal mode.
note: I am using Intellij, testNG.
Class
public class ProjectDao {
private static Logger LOG = LogManager.getLogger(ProjectDao.class);
private DataSource dataSource;
private int id;
private String name;
// For Testing
public ProjectDao(DataSource dataSource) {
this.dataSource = dataSource;
}
public ProjectDao() {
dataSource = Database.getDataSource();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Project> getProjects() throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String SQL = "SELECT project.id, project.name FROM cicd.project";
List<Project> projects = new ArrayList<>();
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(SQL);
rs = ps.executeQuery();
while (rs.next()) {
Project p = new Project();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
projects.add(p);
}
LOG.debug("found: " + projects);
return projects;
} catch (SQLException e) {
LOG.error("Error: " + e.getMessage());
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
LOG.error("Error closing resultset: " + e.getMessage());
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
LOG.error("Error closing PreparedStatement: " + e.getMessage());
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOG.error("Error closing connection: " + e.getMessage());
e.printStackTrace();
}
}
}
return projects;
}
Test class
#PrepareForTest(ProjectDao.class)
public class ProjectDaoTests {
private static Logger LOG = LogManager.getLogger(ProjectDaoTests.class);
List<Project> projects;
Project projectA = new Project();
Project projectB = new Project();
#Mock
DataSource mockDataSource;
#Mock
Connection mockConn;
#Mock
PreparedStatement mockPreparedStmnt;
#Mock
ResultSet mockResultSet;
#BeforeTest(groups = "ut")
public void initMocks() throws IOException, SQLException {
MockitoAnnotations.initMocks(this);
when(mockDataSource.getConnection()).thenReturn(mockConn);
when(mockDataSource.getConnection(anyString(), anyString())).thenReturn(mockConn);
doNothing().when(mockConn).commit();
when(mockConn.prepareStatement(anyString())).thenReturn(mockPreparedStmnt);
when(mockPreparedStmnt.executeQuery()).thenReturn(mockResultSet);
projects = new ArrayList<>();
}
#Test(groups = "ut")
public void testProjects() throws Exception {
projectA.setId(1);
projectA.setName("projectA");
projectB.setId(2);
projectB.setName("projectB");
projects.add(projectA);
projects.add(projectB);
Mockito.when(mockResultSet.next()).thenReturn(true, true, false);
Mockito.when(mockResultSet.getInt("id")).thenReturn(projectA.getId(), projectB.getId());
Mockito.when(mockResultSet.getString("name")).thenReturn(projectA.getName(), projectB.getName());
ProjectDao projectDao = new ProjectDao(mockDataSource);
List<Project> projectsList = projectDao.getProjects();
Assert.assertEquals(projectsList.size(), 2);
Assert.assertEquals(projectsList.get(0).getId(), projectA.getId());
Assert.assertEquals(projectsList.get(0).getName(), projectA.getName());
Assert.assertEquals(projectsList.get(1).getId(), projectB.getId());
Assert.assertEquals(projectsList.get(1).getName(), projectB.getName());
}
Related
This question already has answers here:
Why I obtain this "SQLSyntaxErrorException: ORA-00933: SQL command not properly ended" when I try to perform this JDBC query?
(4 answers)
Closed 1 year ago.
I am trying to connect to the Oracle 19c database from Java. Connection is done and successful.
I'm running same query in SQL Develeper and it's working .But I'm getting exception in prepared Statement and executeQuery() in Java. Please help me out.
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try{
//Loading Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Loading Connection
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521/orcl",dbUsername,dbPassword);
//Executing query;
System.out.println("Connection Established");
//Object to store result
items=new ArrayList<Object>();
queryString="SELECT * FROM HOMESQUAD WHERE EMP_MOBILE_NO='8308856606';";
System.out.println("Query: "+queryString);
if(queryString != null && !queryString.trim().equals("")) {
System.out.println("Into P Statement If Loop");
pstmt=con.prepareStatement(queryString);
System.out.println("StateMent Prepared: "+pstmt.toString());
rs=pstmt.executeQuery();
System.out.println("Query Executed");
while(rs!=null &&rs.next()) {
items.add(constructLoginProps(rs));
}
if(rs !=null) {
rs.close();
}
if(pstmt!=null) {
pstmt.close();
}
}
}
catch(Exception e) {
System.out.println("Exception Occured in aDBExecute:"+e.getMessage());
}
//constructLoginProps Function
private HomesquadEmployee constructLoginProps(ResultSet rs) {
HomesquadEmployee vo=new HomesquadEmployee();
try {
if(rs.getString("EMP_PASSWORD")!=null) {
vo.setEmpPassword(rs.getString("EMP_PASSWORD"));
}
if(rs.getString("EMP_ID")!=null) {
vo.setEmpId(rs.getString("EMP_ID"));
}
if(rs.getString("EMP_NAME")!=null) {
vo.setEmpName(rs.getString("EMP_NAME"));
}
if(rs.getString("EMP_MOBILE_NO")!=null) {
vo.setEmpMobileNo(rs.getString("EMP_MOBILE_NO"));
}
}
catch(Exception e) {
System.out.println("Exception Occured in buildLoginQuery: "+e.getMessage());
}
return(vo);
}
//HomesquadEmployee Class
public class HomesquadEmployee {
private String empId;
private String empName;
private String empMobileNo;
private String empPassword;
public HomesquadEmployee() {
}
public HomesquadEmployee(String empId, String empName, String empMobileNo, String empPassword) {
this.empId = empId;
this.empName = empName;
this.empMobileNo = empMobileNo;
this.empPassword = empPassword;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpMobileNo() {
return empMobileNo;
}
public void setEmpMobileNo(String empMobileNo) {
this.empMobileNo = empMobileNo;
}
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
}
Output:
Connection Established
Query:SELECT * FROM HOMESQUAD_EMPLOYEE WHERE EMP_MOBILE_NO ='8308856606';
Into P Statement If Loop
StateMent Prepared: oracle.jdbc.driver.OraclePreparedStatementWrapper#51aeb3e7
Exception Occured in aDBExecute:ORA-00933: SQL command not properly ended
I had that problem once using an oracle database and I solved it by simply removing the semicolon ";" from the end of the query.
in your case you should change this
queryString="SELECT * FROM HOMESQUAD WHERE EMP_MOBILE_NO='8308856606';";
for this
queryString="SELECT * FROM HOMESQUAD WHERE EMP_MOBILE_NO='8308856606'";
I'm using Oracle 19c database. And HOMESQUAD_EMPLOYEE is table with columns : EMP_ID,EMP_NAME,EMP_MOBILE_NO,EMP_PASSWORD with VARCHAR2(50).
I'm trying to retrieve the row with given EMP_MOBILE_NO in Java using ResultSet object .
Connection is established successfully. PreparedStatement is getting executed successfully,ResultSet is not null but ResultSet.next() is null.
The same query is getting executed and I'm getting required row.
Here's the Code and its output.
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//Loading Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Loading Connection
con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521/orcl", dbUsername, dbPassword);
//Executing query;
System.out.println("Connection Established");
//Object to store result
items = new ArrayList < Object > ();
Map properties={ActionType=ProcessLogin, mobileNo=8308856606};
queryString=buildLoginQuery(properties);
System.out.println("Query: " + queryString);
if (queryString != null && !queryString.trim().equals("")) {
System.out.println("Into P Statement If Loop");
pstmt = con.prepareStatement(queryString);
System.out.println("StateMent Prepared: " + pstmt.toString());
rs = pstmt.executeQuery();
System.out.println("Query Executed");
System.out.println("Result Set rs.next():" + rs.next()); //Error Point
while (rs != null && rs.next()) {
items.add(constructLoginProps(rs));
}
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
}
} catch (Exception e) {
System.out.println("Exception Occured in aDBExecute:" + e.getMessage());
}
//constructLoginProps Function
private HomesquadEmployee constructLoginProps(ResultSet rs) {
HomesquadEmployee vo = new HomesquadEmployee();
try {
if (rs.getString("EMP_PASSWORD") != null) {
vo.setEmpPassword(rs.getString("EMP_PASSWORD"));
}
if (rs.getString("EMP_ID") != null) {
vo.setEmpId(rs.getString("EMP_ID"));
}
if (rs.getString("EMP_NAME") != null) {
vo.setEmpName(rs.getString("EMP_NAME"));
}
if (rs.getString("EMP_MOBILE_NO") != null) {
vo.setEmpMobileNo(rs.getString("EMP_MOBILE_NO"));
}
} catch (Exception e) {
System.out.println("Exception Occured in buildLoginQuery: " + e.getMessage());
}
return (vo);
}
//HomesquadEmployee Class
public class HomesquadEmployee {
private String empId;
private String empName;
private String empMobileNo;
private String empPassword;
public HomesquadEmployee() {}
public HomesquadEmployee(String empId, String empName, String empMobileNo, String empPassword) {
this.empId = empId;
this.empName = empName;
this.empMobileNo = empMobileNo;
this.empPassword = empPassword;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpMobileNo() {
return empMobileNo;
}
public void setEmpMobileNo(String empMobileNo) {
this.empMobileNo = empMobileNo;
}
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
}
//build Query Function
private String buildLoginQuery(Map properties) throws Exception {
String mobileNo="";
String password="";
queryString="SELECT * FROM HOMESQUAD_EMPLOYEE ";
if(properties!=null && (String)properties.get("mobileNo")!=null) {
mobileNo=((String)properties.get("mobileNo")).trim();
queryString=queryString+" WHERE EMP_MOBILE_NO = "+"'"+mobileNo.toUpperCase()+"'";
}
return(queryString);
}
Output:
Connection Established.
Query: SELECT * FROM HOMESQUAD_EMPLOYEE WHERE EMP_MOBILE_NO = '8308856606'
Into P Statement If Loop
StateMent Prepared: oracle.jdbc.driver.OraclePreparedStatementWrapper#514f51cf
Query Executed
Result Set rs.next():false
Please help me out.
I'm working on a sample user-based java web application. When I try to add a user via registration to my database. After capturing the data on the front end, the records get stored as null values
i.e a row of my users table would contain the values (7, null, null, null, null) (using template id(primary-key), f_name, l_name, email, password).
Below are the files I believe could cause possible problems
User.java
package com.ysg.data;
import com.ysg.service.PasswordEncrypter;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class User implements Serializable {
private String id;
private String password;
private String firstName;
private String lastName;
private String email;
public User(){
// default
}
public User( String password, boolean isEncrypted, String firstName, String
lastName, String email){
this.password = isEncrypted ? password : encrypt(password);
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
} //getters and setters follow
UserRepository.java
package com.ysg.repository;
import com.ysg.data.User;
import com.ysg.exception.DuplicateItemException;
import com.ysg.exception.RepositoryException;
import com.ysg.util.MySQLHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserRepository implements RepositoryInt<User> {
private static final String INSERT = "INSERT INTO ysg.users (f_name, l_name, email, password) values (?, ?, ?, ?)";
private static final String USER_LOGIN = "SELECT * FROM ysg.users WHERE email=? and password=?";
private static final String USER_FETCH = "SELECT * FROM ysg.users WHERE email='";
private static final String FIRSTNAME = "f_name";
private static final String LASTNAME = "l_name";
private static final String EMAIL= "email";
private static final String PASSWORD = "password";
public UserRepository(){
// default
}
#Override
public User login(User obj){
User result = find(obj);
if (result != null) {
return result;
}
return null;
}
#Override
public User register(User obj){
if (obj != null && (find(obj) == null)) {
return add(obj);
}
return null;
}
private User add(User user){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(INSERT);
stmt.setString(1, user.getFirstName());
stmt.setString(2, user.getLastName());
stmt.setString(3, user.getEmail());
stmt.setString(4, user.getPassword());
stmt.execute();
closeStatement(stmt);
return user;
}
catch (SQLException ex) {
if (ex.getMessage().contains("Duplicate")) {
System.out.println("Item with duplicate id already exists in repository");
throw new DuplicateItemException("Item with duplicate id already exists in repository");
}
else {
ex.printStackTrace();
System.out.println("Failed to add item to repository");
throw new RepositoryException(ex);
}
}
finally {
closeConnection(conn);
}
}
private User fetch(User user){
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.execute(USER_FETCH + user.getEmail()+ "'");
ResultSet resultSet = stmt.getResultSet();
User result = marshall(resultSet);
closeStatement(stmt);
return result;
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("Failed to fetch item from repository");
throw new RepositoryException(ex);
}
finally {
closeConnection(conn);
}
}
private User find(User user){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(USER_LOGIN);
stmt.setString(1, user.getEmail());
stmt.setString(2, user.getPassword());
ResultSet resultSet = stmt.executeQuery();
User result = marshall(resultSet);
closeStatement(stmt);
return result;
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("Failed to add item to repository");
throw new RepositoryException(ex);
}
finally {
closeConnection(conn);
}
}
private User marshall(ResultSet result) throws RepositoryException, SQLException{
if (result == null) {
return null;
}
User user = null;
if (result.next()) {
// String id = result.getString(ID);
String password = result.getString(PASSWORD);
String firstName = result.getString(FIRSTNAME);
String lastName = result.getString(LASTNAME);
String email = result.getString(EMAIL);
user = new User(password, true, firstName, lastName, email);
}
return user;
}
private Connection getConnection(){
return MySQLHelper.getConnection();
}
private static void closeStatement(Statement stmt){
if (stmt != null) {
try {
stmt.close();
//stmt = null;
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
private void closeConnection(Connection conn){
if (conn != null) {
try {
conn.close();
conn = null;
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
MySQLHelper.java
package com.ysg.util;
import com.ysg.data.User;
import com.ysg.exception.DuplicateItemException;
import com.ysg.exception.RepositoryException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class MySQLHelper {
private static final String INSERT_USER = "INSERT INTO users(f_name, l_name, email, password) values (?, ?, ?, ?)";
private static final String USER_FETCH = "SELECT * FROM users WHERE id='";
private static final String FETCH_ALL = "SELECT * FROM users";
private static final String COUNT_ALL_USERS = "SELECT COUNT(*) FROM users";
private static final String DELETE_ALL = "DELETE FROM users";
private static final String ID = "id";
private static final String EMAIL = "email";
private static final String PASSWORD = "password";
private static final String FIRSTNAME = "f_name";
private static final String LASTNAME = "l_name";
private static final String DS_NAME = "jdbc/sen301DS";
private static Context envCtx;
private static DataSource ds;
static {
try {
Context ctx = new InitialContext();
envCtx = (Context) ctx.lookup("java:/comp/env");
System.out.println(envCtx);
}
catch (Exception ex) {
System.out.println("ConnectionPool will not be available - only direct connection can be used");
}
}
public static List<User> fetch() throws SQLException{
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.execute(FETCH_ALL);
ResultSet resultSet = stmt.getResultSet();
List<User> result = marshall(resultSet);
closeStatement(stmt);
return result;
}
catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
finally {
closeConnection(conn);
}
}
public static User fetch(String id) throws SQLException{
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.execute(USER_FETCH + id + "'");
ResultSet resultSet = stmt.getResultSet();
List<User> result = marshall(resultSet);
closeStatement(stmt);
return result.get(0);
}
catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
finally {
closeConnection(conn);
}
}
public static User add(User user){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(INSERT_USER);
stmt.setString(1, user.getFirstName());
stmt.setString(2, user.getLastName());
stmt.setString(3, user.getEmail());
stmt.setString(4, user.getPassword());
stmt.execute();
closeStatement(stmt);
return user;
}
catch (SQLException ex) {
if (ex.getMessage().contains("Duplicate")) {
System.out.println("Item with duplicate id already exists in repository");
throw new DuplicateItemException("Item with duplicate id already exists in repository");
}
else {
ex.printStackTrace();
System.out.println("Failed to add item to repository");
throw new RepositoryException(ex);
}
}
finally {
closeConnection(conn);
}
}
public static int countusers() throws SQLException{
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.execute(COUNT_ALL_USERS);
ResultSet resultSet = stmt.getResultSet();
int count = 0;
if (resultSet.next()) {
count = resultSet.getInt(1);
}
closeStatement(stmt);
return count;
}
catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
finally {
closeConnection(conn);
}
}
public static void reset() throws SQLException{
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(DELETE_ALL);
closeStatement(stmt);
}
catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
finally {
closeConnection(conn);
}
}
private static List<User> marshall(ResultSet result) throws SQLException{
List<User> list = new ArrayList<User>();
if (result == null) {
return list;
}
while (result.next()) {
String firstName = result.getString(FIRSTNAME);
String lastName = result.getString(LASTNAME);
String email = result.getString(EMAIL);
String passwd = result.getString(PASSWORD);
User user = new User(passwd, true, firstName, lastName, email);
list.add(user);
}
return list;
}
private static DataSource getDataSource(){
DataSource ds = null;
try {
ds = (DataSource) envCtx.lookup(DS_NAME);
}
catch (NamingException ex) {
ex.printStackTrace();
throw new RepositoryException("Failed to get DataSource");
}
return ds;
}
public static Connection getConnection(){
Connection con = null;
try {
con = getDataSource().getConnection();
}
catch (SQLException ex) {
ex.printStackTrace();
throw new RepositoryException("Failed to get connection to database server");
}
catch (Exception ex) {
con = getDirectConnection();
}
return con;
}
private static Connection getDirectConnection(){
try {
String conURL = "jdbc:mysql://localhost:3306/ysg?serverTimezone=UTC&useSSL=false&" + "user=sen301&password=sen301";
return DriverManager.getConnection(conURL);
}
catch (SQLException ex) {
ex.printStackTrace();
throw new RepositoryException("Failed to get direct connection to database server");
}
catch (Exception ex) {
ex.printStackTrace();
throw new RepositoryException(ex);
}
}
private static void closeStatement(Statement stmt){
if (stmt != null) {
try {
stmt.close();
stmt = null;
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
private static void closeConnection(Connection conn){
if (conn != null) {
try {
conn.close();
conn = null;
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
If I use the Hibernate Tools plugin to generate the entity/model and DAO to connect to a mysql database, how do I instantiate the DAO and use CRUD operation without using Spring?
my model is:
#Entity #Table(name = "misurazione", catalog = "fitness")
public class Misurazione implements java.io.Serializable {
private static final long serialVersionUID = 1L;
// query name references
private Integer idMisurazione;
private Monitoraggio monitoraggio;
private String nomeMisurazione;
private float minValore;
private float maxValore;
private Set<Rilevazione> rilevaziones = new HashSet<Rilevazione>(0);
private Set<ComposizioneMisurazioneDispositivo> composizioneMisurazioneDispositivos = new HashSet<ComposizioneMisurazioneDispositivo>(
0);
private Set<ComposizioneMisurazioneTipoMisurazione> composizioneMisurazioneTipoMisuraziones = new HashSet<ComposizioneMisurazioneTipoMisurazione>(
0);
public static String REF = "Misurazione";
public static final String PROP_idMisurazione = "idMisurazione";
public static final String PROP_monitoraggio = "monitoraggio";
public static final String PROP_nomeMisurazione = "nomeMisurazione";
public static final String PROP_minValore = "minValore";
public static final String PROP_maxValore = "maxValore";
public static final String PROP_rilevaziones = "rilevaziones";
public static final String PROP_composizioneMisurazioneDispositivos = "composizioneMisurazioneDispositivos";
public static final String PROP_composizioneMisurazioneTipoMisuraziones = "composizioneMisurazioneTipoMisuraziones";
public Misurazione() {
}
public Misurazione(Monitoraggio monitoraggio, String nomeMisurazione,
float minValore, float maxValore) {
this.monitoraggio = monitoraggio;
this.nomeMisurazione = nomeMisurazione;
this.minValore = minValore;
this.maxValore = maxValore;
}
public Misurazione(
Monitoraggio monitoraggio,
String nomeMisurazione,
float minValore,
float maxValore,
Set<Rilevazione> rilevaziones,
Set<ComposizioneMisurazioneDispositivo> composizioneMisurazioneDispositivos,
Set<ComposizioneMisurazioneTipoMisurazione> composizioneMisurazioneTipoMisuraziones) {
this.monitoraggio = monitoraggio;
this.nomeMisurazione = nomeMisurazione;
this.minValore = minValore;
this.maxValore = maxValore;
this.rilevaziones = rilevaziones;
this.composizioneMisurazioneDispositivos = composizioneMisurazioneDispositivos;
this.composizioneMisurazioneTipoMisuraziones = composizioneMisurazioneTipoMisuraziones;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id_misurazione", unique = true, nullable = false)
public Integer getIdMisurazione() {
return this.idMisurazione;
}
public void setIdMisurazione(Integer idMisurazione) {
this.idMisurazione = idMisurazione;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_monitoraggio", nullable = false)
public Monitoraggio getMonitoraggio() {
return this.monitoraggio;
}
public void setMonitoraggio(Monitoraggio monitoraggio) {
this.monitoraggio = monitoraggio;
}
#Column(name = "nome_misurazione", nullable = false, length = 15)
public String getNomeMisurazione() {
return this.nomeMisurazione;
}
public void setNomeMisurazione(String nomeMisurazione) {
this.nomeMisurazione = nomeMisurazione;
}
#Column(name = "min_valore", nullable = false, precision = 5)
public float getMinValore() {
return this.minValore;
}
public void setMinValore(float minValore) {
this.minValore = minValore;
}
#Column(name = "max_valore", nullable = false, precision = 5)
public float getMaxValore() {
return this.maxValore;
}
public void setMaxValore(float maxValore) {
this.maxValore = maxValore;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "misurazione")
public Set<Rilevazione> getRilevaziones() {
return this.rilevaziones;
}
public void setRilevaziones(Set<Rilevazione> rilevaziones) {
this.rilevaziones = rilevaziones;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "misurazione")
public Set<ComposizioneMisurazioneDispositivo> getComposizioneMisurazioneDispositivos() {
return this.composizioneMisurazioneDispositivos;
}
public void setComposizioneMisurazioneDispositivos(
Set<ComposizioneMisurazioneDispositivo> composizioneMisurazioneDispositivos) {
this.composizioneMisurazioneDispositivos = composizioneMisurazioneDispositivos;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "misurazione")
public Set<ComposizioneMisurazioneTipoMisurazione> getComposizioneMisurazioneTipoMisuraziones() {
return this.composizioneMisurazioneTipoMisuraziones;
}
public void setComposizioneMisurazioneTipoMisuraziones(
Set<ComposizioneMisurazioneTipoMisurazione> composizioneMisurazioneTipoMisuraziones) {
this.composizioneMisurazioneTipoMisuraziones = composizioneMisurazioneTipoMisuraziones;
}
/**
* toString
* #return String
*/
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().getName()).append("#")
.append(Integer.toHexString(hashCode())).append(" [");
buffer.append("idMisurazione").append("='").append(getIdMisurazione())
.append("' ");
buffer.append("monitoraggio").append("='").append(getMonitoraggio())
.append("' ");
buffer.append("nomeMisurazione").append("='")
.append(getNomeMisurazione()).append("' ");
buffer.append("minValore").append("='").append(getMinValore())
.append("' ");
buffer.append("maxValore").append("='").append(getMaxValore())
.append("' ");
buffer.append("rilevaziones").append("='").append(getRilevaziones())
.append("' ");
buffer.append("composizioneMisurazioneDispositivos").append("='")
.append(getComposizioneMisurazioneDispositivos()).append("' ");
buffer.append("composizioneMisurazioneTipoMisuraziones").append("='")
.append(getComposizioneMisurazioneTipoMisuraziones())
.append("' ");
buffer.append("]");
return buffer.toString();
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof Misurazione))
return false;
Misurazione castOther = (Misurazione) other;
return false;
}
public int hashCode() {
int result = 17;
return result;
}
}
And DAO IS:
import it.neatec.hl7.db.DaoInterface;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MisurazioneDao extends HibernateDaoSupport implements
DaoInterface<Misurazione> {
private static final Logger logger = Logger.getLogger(MisurazioneDao.class);
public Session getCurrentSession() {
try {
return (Session) getHibernateTemplate().getSessionFactory()
.getCurrentSession();
} catch (Exception e) {
logger.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public Class getReferenceClass() {
return Misurazione.class;
}
public void save(Misurazione istance) {
logger.debug("Save Misurazione instance");
try {
getHibernateTemplate().save(istance);
logger.debug("Save successful");
} catch (RuntimeException re) {
logger.error("Save failed", re);
throw re;
}
}
public void update(Misurazione istance) {
logger.debug("Update Misurazione instance");
try {
getHibernateTemplate().update(istance);
logger.debug("Update successful");
} catch (RuntimeException re) {
logger.error("Update failed", re);
throw re;
}
}
public void saveOrUpdate(Misurazione instance) {
logger.debug("saveOrUpdate Misurazione instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
logger.debug("saveOrUpdate successful");
} catch (RuntimeException re) {
logger.error("saveOrUpdate failed", re);
throw re;
}
}
public void deleteAll(Collection<Misurazione> entities) {
logger.debug("delete collection Misurazione entities");
try {
getHibernateTemplate().deleteAll(entities);
logger.debug("delete successful");
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}
public void delete(Misurazione instance) {
logger.debug("deleting Misurazione instance");
try {
getHibernateTemplate().delete(instance);
logger.debug("delete successful");
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}
public Misurazione merge(Misurazione instance) {
logger.debug("merging Misurazione instance");
try {
Misurazione result = (Misurazione) getHibernateTemplate().merge(
instance);
logger.debug("merge successful");
return result;
} catch (RuntimeException re) {
logger.error("merge failed", re);
throw re;
}
}
public Misurazione findById(java.lang.Integer id) {
logger.debug("getting Misurazione instance with id: " + id);
try {
Misurazione instance = (Misurazione) getHibernateTemplate().get(
Misurazione.class, id);
if (instance == null) {
logger.debug("get successful, no instance found");
} else {
logger.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
logger.error("get failed", re);
throw re;
}
}
public Misurazione findById(Serializable id) {
logger.debug("getting Misurazione instance with id: " + id);
try {
Misurazione instance = (Misurazione) getHibernateTemplate().get(
Misurazione.class, id);
if (instance == null) {
logger.debug("get successful, no instance found");
} else {
logger.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
logger.error("get failed", re);
throw re;
}
}
public List<Misurazione> loadAll() {
logger.debug("loadAll Misurazione instance ");
try {
List<Misurazione> results = (List<Misurazione>) getHibernateTemplate()
.loadAll(Misurazione.class);
logger.debug("loadAll successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
logger.error("loadAll failed", re);
throw re;
}
}
public List<Misurazione> findByCriteria(Criterion... criterion) {
logger.debug("finding Misurazione instance by Criteria");
try {
Criteria crit = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(Misurazione.class);
for (Criterion c : criterion) {
crit.add(c);
}
List<Misurazione> results = (List<Misurazione>) crit.list();
logger.debug("find by Criterion successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
logger.error("find by Criterion failed", re);
throw re;
}
}
public List<Misurazione> findByCriteria(DetachedCriteria dc, int from,
int size) {
logger.debug("finding Misurazione instance by DetachedCriteria");
try {
List<Misurazione> results = (List<Misurazione>) getHibernateTemplate()
.findByCriteria(dc, from, size);
logger.debug("find by Criterion successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
logger.error("find by DetachedCriteria failed", re);
throw re;
}
}
public Integer countByCriteria(final DetachedCriteria dc) {
logger.debug("count Misurazione");
try {
Integer count = (Integer) getHibernateTemplate()
.executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session)
throws RuntimeException {
Criteria executableCriteria = dc
.getExecutableCriteria(session);
executableCriteria.setProjection(Projections
.rowCount());
for (Object result : executableCriteria.list()) {
if (result instanceof Integer) {
return (Integer) result;
} else if (result instanceof Long) {
return ((Long) result).intValue();
}
}
return -1;
}
});
return count.intValue();
} catch (RuntimeException e) {
logger.error(e.getMessage(), e);
throw e;
}
}
public List<Misurazione> findByExample(Misurazione instance) {
logger.debug("finding Misurazione instance by example");
try {
List<Misurazione> results = (List<Misurazione>) getHibernateTemplate()
.findByExample(instance);
logger.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
logger.error("find by example failed", re);
throw re;
}
}
}
Thank you
I resolved in this way, regenerate (hibernate tools) dao with different daohome.ftl that include HiberneteUtil for start session in all CRUD operation.
After I use a ServiceFactory for read applicationContext.xml where I have put the whole list of annotaded Hibernate class
I'm making a Timetable scheduler as a final year project. For the last two days, I'm getting an OutOfMemoryException. I have read a lot about the exception, and tried to increase the memory alloted through the -Xms and -Xmx options. None of these seem to work for me.
I profiled the project, and found that the maximum space was consumed by hashmap objects, and also by the MySQL connection. I have used a static connection as follows
public final class Connector
{
private static Connector connector;
Connection con;
String driverName;
String dbname;
String username;
String password;
String connString;
private Connector(){
driverName = "com.mysql.jdbc.Driver";
dbname = "timegen";
username = "root";
password = "root";
connString = "jdbc:mysql://localhost:3306/" + dbname;
openConnection();
}
public void openConnection(){
try{
Class.forName(driverName);
con = DriverManager.getConnection(connString, username, password);
} catch(Exception e){
System.out.println(e);
}
}
public void terminateConnection(){
try{
con.close();
} catch(Exception e){
System.out.println(e);
}
}
public static Connector createConnection() {
if (connector == null){
connector = new Connector();
}
return connector;
}
public Connection getCon() {
return con;
}
public String getConnString() {
return connString;
}
public void setConnString(String connString) {
this.connString = connString;
}
}
This is the code for a class named MasterData, which is extended by all other classes that access the database
public class MasterData{
static Connector con;
static Statement st;
MasterData(){
try {
con = Connector.createConnection();
st = con.getCon().createStatement();
} catch (SQLException ex) {
Logger.getLogger(MasterData.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Statement createStatement() throws SQLException{
Statement st = con.getCon().createStatement();
return st;
}
public void closeConnection(){
con.terminateConnection();
}
}
An example of a class that uses this
public class Teacher extends MasterData{
int teacherid;
String teachername;
String subject;
String post;
#Override
public String toString() {
return "Teacher{" + "teacherid=" + teacherid + ", teachername=" + teachername + ",
post=" + post + ", subject=" + subject + '}';
}
public Teacher(int teacherid, String teachername,String subject, String post) {
this.teacherid = teacherid;
this.teachername = teachername;
this.subject = subject;
this.post = post;
}
public Teacher(String teachername) {
this.teachername = teachername;
}
public Teacher(){}
public String display(){
String s ="\nTeacher name = " + teachername
+ "\nSubject = " + subject
+ "\nPost = "+post;
return s;
}
public ArrayList<String> getSubjectTeachers(String s){
ArrayList<String> teachers = new ArrayList<String>();
try{
ResultSet rs = st.executeQuery("select teachername from teacher where
subject='"+s+"';");
while(rs.next()){
teachers.add(rs.getString(1));
}
}catch(Exception e){e.printStackTrace();}
return teachers;
}
public List<Teacher> getFree()
{
List<Teacher> lst = new ArrayList<Teacher>();
try{
ResultSet rs = st.executeQuery("select * from teacher where teacherid not
in(select classteacher from division where classteacher!=null)");
while(rs.next())
{
lst.add(new
Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)));
}
}catch(Exception e ){e.printStackTrace();}
return lst;
}
public int getTeacherid() {
return teacherid;
}
public void setTeacherid(int teacherid) {
this.teacherid = teacherid;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public boolean checkDuplicate(){
try{
ResultSet rs = st.executeQuery("select * from teacher where
teachername='"+teachername+"';");
if(rs.next())
return true;
}catch(Exception e){e.printStackTrace();}
return false;
}
public boolean insert(){
int t;
try{
t = st.executeUpdate("insert into teacher(teachername,subject,post)
values('"+teachername+"','"+subject+"','"+post+"');");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
public boolean delete(){
int t;
try{
new AssignedTeacher().deleteTeacher(teacherid);
t = st.executeUpdate("delete from teacher where teacherid="+teacherid+";");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
public boolean update(){
int t;
try{
t = st.executeUpdate("update teacher set teachername = '"+teachername+"',
subject='"+subject+"', post='"+post+"' where teacherid="+teacherid+";");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
}
My intention was to create a single static connection for the entire program. It seems to work well. But is this the possible cause of the problem?
Try to set these parameters as well:
-XX:PermSize
-XX:MaxPermSize
It looks like you are creating too many Connections.
You may verify whether or not your connection is valid in your openConnection method You also may use some Connection Pool.
EDIT:
It seems to me that you've tried to implement Active record pattern because there are insert, delete, update and getSubjectTeachers methods. Anyway, its is not always a good idea to extend Teacher from MasterData. As a side effect, new connections will be created for each instance of MasterData. static Connection con would be reassigned to new object but previous Connection will not be ever closed. Same holds with MasterData#createStatement.
Also, as greedybuddha pointed out, make sure that your HashMap are not reassigned in the same manner.