On JPA CurdRepository, what happen if the Domain ID type has wrongly input to Integer, while the correct main id should be varchar from Database? Will it trigger a "failsafe" connection to database while the configuration will restore back to last successful connection?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Short answer
If the @Id field of your entity is declared as Integer while the physical column in the database is VARCHAR, nothing “auto-repairs” or silently “rolls back to the last good connection”.
Instead, you’ll hit runtime errors as soon as Hibernate (or any other JPA provider) tries to read a non-numeric value from that column, or you will get subtle data-type mismatches depending on your DBMS.
What exactly happens?
Mapping time
java
@Entity
public class Customer {
@Id // <-- mapped as INTEGER in the metadata
private Integer id;
...
}
Hibernate builds its metadata assuming id is a numeric column.
• If spring.jpa.hibernate.ddl-auto = update, Hibernate may try to alter the column to an INT/NUMBER behind your back.
• If DDL is off (most prod setups), Hibernate keeps going, believing the DB column is numeric.
Write / insert
• When you persist a new entity with id = 5, Hibernate binds an integer to the prepared statement.
• Most databases will happily coerce 5 → '5' and store it in a VARCHAR. So inserts may actually succeed.
Read / select
While building objects, Hibernate calls ResultSet#getInt("id").
• If the row contains '17', the driver converts the text to an int → OK.
• The first row that contains 'ABC17', 'CUS001', or any non-numeric char triggers:
code
java.sql.SQLDataException: Cannot convert value 'CUS001' to INTEGER
at ...
org.hibernate.exception.DataException: could not extract ResultSet
No fallback, no reconnection magic—your transaction fails and the stack trace bubbles up.
Connection / pool
The JDBC connection itself is fine; the exception is at the data-conversion layer. Your pool (Hikari, etc.) is not restarted, and “last successful connection” is irrelevant here.
Edge cases
• MySQL’s “permissive” mode might silently coerce some text to 0; you end up with id=0 rows—data corruption territory.
• Numeric generators (@GeneratedValue(strategy = IDENTITY/SEQUENCE)) can’t be used when you later switch the column to true strings.
How to fix
• Make the Java type match the DB column:
java
@Id
private String id;
(or UUID, Long, etc., depending on your true PK design).
• If you need both numeric and text values, keep the column VARCHAR and implement a custom IdentifierGenerator or assign IDs manually.
• Run integration tests that hit a real database (Testcontainers, Docker, etc.)—mismatches like this show up immediately.
Take-away
JPA will not “restore” anything if you declare the wrong Java type. You’ll either get data-conversion exceptions at runtime or, worse, silent data corruption on permissive databases. Always keep your entity field types in sync with the underlying column definitions.
User
What happen if:
@Entity
public class Customer {
@Id
private Integer id;
// other fields, getters, and setters
}
@Entity
public class Customer {
@Id
private Integer id;
…
}
But the repository is declared with String as its ID type:
java
public interface CustomerRepository
extends CrudRepository<Customer, String>, // <-- wrong
JpaSpecificationExecutor<Customer> {
Customer findById(int id); // own method
}
Generics are checked only at compile time, and Spring Data does not cross-check the repository’s <ID> type with the entity’s @Id type.
So the project compiles and the application starts.
What happens at runtime
A. Built-in CRUD methods (those that come from CrudRepository)
• findById(String id), existsById(String id), deleteById(String id) …
All of them delegate to EntityManager.find(Customer.class, id).
• When you call any of these with a String, Hibernate/JPA looks at the
entity metadata, sees that the real PK type is Integer, and throws:
code
```
java.lang.IllegalArgumentException:
Provided id of the wrong type for entity Customer.
Expected: class java.lang.Integer, got class java.lang.String
```
• No “failsafe”, no automatic reconnect. The stack trace bubbles up and
your current transaction is rolled back (normal Spring behaviour).
B. Your own overload Customer findById(int id)
• Spring Data treats this as a derived query method.
It builds JPQL select c from Customer c where c.id = ?1.
• The int argument is auto-boxed to Integer, matches the column type,
and the query executes successfully.
• Result: you now have two different findById methods in the same
interface—one that works (the derived query with int) and one that
always fails (the inherited CRUD method with ). Confusing!
String
C. Database / connection pool
• The JDBC connection itself is perfectly fine—the failure happens before
any row is fetched.
• HikariCP, Tomcat-JDBC, etc. do not “restore the last good connection”,
because there was nothing wrong with the connection.
Summary
• The code compiles but is inconsistent.
• All default CRUD methods will throw IllegalArgumentException at runtime
because the ID value you pass (String) does not match the entity’s ID
type (Integer).
• Your ad-hoc findById(int) works, but having two homonymous methods is an
accident waiting to happen.
Fix
java
public interface CustomerRepository
extends CrudRepository<Customer, Integer>, // <- correct ID type
JpaSpecificationExecutor<Customer> {
// Optional derived query if you still want it
Customer findById(Integer id);
}
Always keep the repository’s <ID> generic type in sync with the @Id
field of the entity. That prevents both runtime errors and developer
confusion.