Thinker
Thinker

Reputation: 6892

Gettng exception while using findBy using JPARepository?

I have 2 tables Employee and Address. Each have primary key sequence_id and address_Id which is generated through sequence.

Employee Class:

public class Employee {
    @Id
    @Column(name = "sequence_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
    @SequenceGenerator(sequenceName = "emp_seq", allocationSize = 1, name = "emp_seq")
    private Long sequenceId;

    @Column(name = "emp_id")
    private String empId;

    @Column(name = "joiningDate")
    private Date businessDate;

    @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
    private Address address;
}

Address Class

public class Address {

@Id
@Column(name = "address_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_seq")
@SequenceGenerator(sequenceName = "address_seq", allocationSize = 1, name = "address_seq")
private Long addressID;

@Column(name = "street")
private String street;

@OneToOne
@JoinColumn(name = "emp_id", referencedColumnName = "emp_id")
@JsonIgnore
private Employee employee;

}

I am using JPARepository to save both entities. here is my main class.

Employee e = Employee.builder().businessDate(new Date())
        .empId("thinkerId").build();
Address a = Address.builder().street("32nd Street")
        .employee(e).build();
e.setAddress(a);
empRepository.save(e);

Code runs successfully and save both into address and employee table.

But it failed while usig findAll() / findById method?

Optional<Employeee> emp = employeeRepository.findById(23l); //**FAILING**

Error --

[2022.02.08 09:08:13:401] [http-nio-8997-exec-2] [o.h.e.j.s.SqlExceptionHelper] [WARN ] - SQL Error: 1722, SQLState: 42000
[2022.02.08 09:08:13:403] [http-nio-8997-exec-2] [o.h.e.j.s.SqlExceptionHelper] [ERROR] - ORA-01722: invalid number

[2022.02.08 09:08:13:430] [http-nio-8997-exec-2] [o.h.e.i.DefaultLoadEventListener] [INFO ] - HHH000327: Error performing load command
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

While using findAll() method, I got below error:

org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.String com.msci.rt.monitoring.entity.Employee.empId] by reflection for persistent property [com.msci.rt.monitoring.entity.Employee#empId] :
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String Employee.emp_ID] by reflection for persistent property [empId] : 22
    ... 85 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field Employee.empId to null value
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)

How can i get "thinkerId" as value in Address table?

Thank you.

Upvotes: 0

Views: 510

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522626

The error message from Hibernate seems to be saying that the database is expecting a number somewhere, but instead is getting something else, possibly a string. If you examine your Employee entity class, you'll notice that you have defined the join column emp_id to be a string. However the primary key field there is called sequence_id. I suggest making the PK column the join field. Use this version:

public class Employee {
    @Id
    @Column(name = "emp_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
    @SequenceGenerator(sequenceName = "emp_seq", allocationSize = 1, name = "emp_seq")
    private Long empId;

    @Column(name = "joiningDate")
    private Date businessDate;

    @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
    private Address address;
}

Upvotes: 1

Related Questions