0
votes

I'm trying to implement simple Spring Data application. I have simple entity Employee with a few fields and I use method from CrudRepository: findById to retrieve employee from database. But always result is empty, onlny ID field in entity is correct but other fields are empty. Even if I'm trying to get findAll() then I've got all records from table but they have only ID field, nothing else.

I do not use addonations just XML files of Hibernate .hbm.xml. There is how does looks entity with hbm.xml:

Employee entity:

public class Employee extends BaseEntity {
private static final long serialVersionUID = -1400760321767476971L;
private String empFirstName;
private String empPassword;
private String empRole;
private String empLastName;
private String empBirthDate;
private String empAvatar;
private Integer empDepartmentId;
private Integer empEnabled;
private Integer empPositionId;
private Integer empManagerId;
private Integer empIsManager;
private String empEvidenceNumber;
private Double empAreaOfWork;
private Integer empWorkingHoursPerDay;
private String empFirstWorkDay;

...getters/setters/constructors

Employee.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Employee" table="vt_employee" catalog="VacationTool"
    optimistic-lock="version">


    <id name="id" type="java.lang.Integer">
        <column name="id_employee" />
        <generator class="identity" />
    </id>
    <property name="empPassword" type="string">
        <column name="emp_password" length="60" />
    </property>
    <property name="empRole" type="string">
        <column name="emp_role" length="45" />
    </property>
    <property name="empEnabled" type="java.lang.Integer">
        <column name="emp_enabled" />
    </property>
    <property name="empFirstName" type="string">
        <column name="emp_firstName" length="75" />
    </property>
    <property name="empLastName" type="string">
        <column name="emp_lastName" length="75" />
    </property>
    <property name="empBirthDate" type="string">
        <column name="emp_birthDate" length="10" />
    </property>
    <property name="empAvatar" type="string">
        <column name="emp_avatar" />
    </property>
    <property name="empDepartmentId" type="java.lang.Integer">
        <column name="emp_department_id" />
    </property>
    <property name="empPositionId" type="java.lang.Integer">
        <column name="emp_position_id" />
    </property>
    <property name="empManagerId" type="java.lang.Integer">
        <column name="emp_manager_id" />
    </property>
    <property name="empIsManager" type="java.lang.Integer">
        <column name="emp_isManager" />
    </property>
    <property name="empEvidenceNumber" type="string">
        <column name="emp_evidenceNumber" length="4" />
    </property>
    <property name="empAreaOfWork" type="java.lang.Double">
        <column name="emp_areaOfWork" precision="3" />
    </property>
    <property name="empWorkingHoursPerDay" type="java.lang.Integer">
        <column name="emp_workingHoursPerDay" />
    </property>
    <property name="empFirstWorkDay" type="string">
        <column name="emp_firstWorkDay" length="10" />
    </property>


</class>
</hibernate-mapping>

Employee entity extends BaseEntity with only one field "id" with setter/getter.

Employee repository:

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {

}

EmployeeService:

@Service
public class EmpService {
   @Autowired
   private EmployeeRepository employeeRepository;

   public Employee getEmployee(int id) {
       return employeeRepository.findById(id).get();

   }
}

After that i'm trying to use system.out.println in main controller and also display data on simple index page. I'm sure it is not problem of displaying data because everything works fine with Hibernate. All records with Spring Data have only ID field. And there is query from logging in console:

Hibernate: select employee0_.id_employee as id_emplo1_2_0_, employee0_.emp_password as emp_pass2_2_0_, employee0_.emp_role as emp_role3_2_0_, employee0_.emp_enabled as emp_enab4_2_0_, employee0_.emp_first_name as emp_firs5_2_0_, employee0_.emp_last_name as emp_last6_2_0_, employee0_.emp_birth_date as emp_birt7_2_0_, employee0_.emp_avatar as emp_avat8_2_0_, employee0_.emp_department_id as emp_depa9_2_0_, employee0_.emp_position_id as emp_pos10_2_0_, employee0_.emp_manager_id as emp_man11_2_0_, employee0_.emp_is_manager as emp_is_12_2_0_, employee0_.emp_evidence_number as emp_evi13_2_0_, employee0_.emp_area_of_work as emp_are14_2_0_, employee0_.emp_working_hours_per_day as emp_wor15_2_0_, employee0_.emp_first_work_day as emp_fir16_2_0_ from vacation_tool.vt_employee employee0_ where employee0_.id_employee=?

How can I get all fields from database? I have 15 records in my table with full data.

2
What is the code? error? - Rafael Palomino
It sounds like the column names of the database table may not match the field names in the Employee entity class. If you post the code for your table creation and your Employee class we may be able to help. - Wrokar
I added my code - Carath

2 Answers

0
votes

I found the problem. During use my code there wasn't any error/warning but I found one property to add in application.properties:

hibernate.hbm2ddl.auto=update

This line creates missing columns in table so I just wanted to check it. After that in console I saw there are creating new columns for ALL columns with more than one word e.g. in my table I had "emp_firstName" and there were creating new column "emp_first_name". Even if in my .hbm.xml I've declared this field:

<property name="empFirstName" type="string">
    <column name="emp_firstName" length="75" />
</property>

It doesn't work. So when I found this in console logging I changed this column and others using "_" and then everythinng works fine.

But I can't understand two things

1) When I changed columns names BUT not changed properties in .hbm.xml it still works with wrong column name in property file(of course hibernate doesn't work in this case so I changed properties also with "_"). I have no idea what to thing about it?

2) Why there is needed using "_" after each one word e.g. "first_name" in column name instead of e.g. "firstName"?

0
votes

It's a litle bit hard to help without the code, but you can check:

  • is the other fields accessible (setters or public modifiers)?
  • are the fields mapped to the right column? @Column(name="my_column")