У меня есть две таблицы, которые связаны через имя класса (1: n).
Домен: Продукт (1)
Домен: HistoryPrice (n)
Продукт
@Entity
@Table
public class Product extends AbstractBaseDomain<Long> {
@NotBlank
@Size(min = 2, max = 50)
@Column(name ="name", unique = true)
private String name;
HistoryPrice
@Entity
@Table(name = "historyPrice")
public class HistoryPrice extends AbstractBaseDomain<Long> {
@NotNull
@ManyToOne
@JoinColumn(name ="product")
private Product product;
Это мой репозиторий
@Repository
public interface HistoryPriceRepository extends JpaRepository<HistoryPrice, Long> {
@Query(value = "SELECT h.product " +
"FROM history_price h " +
"INNER JOIN product p ON h.product = p.name " +
"WHERE p.name = :name", nativeQuery = true)
List<?> findProductByName(@Param("name") String name);
}
Это мой контроллер
@PostMapping(value = "/historyPrice")
public String searchForProducts(Model model, @RequestParam String namePart) {
List<?> productList = historyPriceService.findProductName(namePart);
model.addAttribute(HISTORYPRICE_VIEW, productList);
return HISTORYPRICE_VIEW;
}
Это мой вывод SQL для создания моей таблицы:
2019-04-11 18:39:20 DEBUG org.hibernate.SQL - create table history_price (id bigint not null, version integer, price decimal(19,2) not null, valid_since timestamp not null, product bigint not null, primary key (id))
2019-04-11 18:39:20 DEBUG org.hibernate.SQL - create table product (id bigint not null, version integer, current_price decimal(19,2) not null, manufacturer varchar(50), name varchar(50), primary key (id))
Это моя сокращенная ошибка, которую я всегда получаю:
Caused by: org.hibernate.exception.DataException: could not extract ResultSet
Caused by: org.h2.jdbc.JdbcSQLException: Datenumwandlungsfehler beim Umwandeln von "HAMMER"
Data conversion error converting "HAMMER"; SQL statement:
SELECT h.product FROM history_price h INNER JOIN product p ON h.product = p.name WHERE p.name = ? [22018-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
at org.h2.message.DbException.get(DbException.java:168)
Caused by: java.lang.NumberFormatException: For input string: "HAMMER"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
Я не знаю, находится ли моя проблема в моем хранилище или где-то еще.
Может быть, кто-то может дать мне правильное решение или хороший совет.
Большое спасибо.