我正在尝试为从抽象类派生的实体实现Jpa存储库。
基础抽象类:
@MappedSuperclass
public abstract class BaseProduct {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String brand;
private int price;
private int availability;
private String description;
//constructors getters and setters omitted
}
派生类:
@Entity
public class Product extends BaseProduct {
//constructors omitted
}
JpaRepository扩展名:
@Repository
public interface ProductRepository extends JpaRepository<Product, Long > {
}
它在哪里破裂:
@Bean
public CommandLineRunner commandLineRunner(ProductRepository productRepository) {
Log log = LogFactory.getLog(this.getClass());
return (args) -> {
ProductInfo p1 = new ProductInfo("Hook1", "lorem", 2, 50, "desc");
ProductInfo p2 = new ProductInfo("Hook2", "lorem", 2, 50, "desc");
Product b1 = new Product(p1);
productRepository.save(b1);
productRepository.save(new Product(p2));
productRepository.findAll().forEach(product -> log.info(product.getName()));
};
这是结果:
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into product (availability, brand, description, name, price, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into product (availability, brand, description, name, price, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select product0_.id as id1_0_, product0_.availability as availabi2_0_, product0_.brand as brand3_0_, product0_.description as descript4_0_, product0_.name as name5_0_, product0_.price as price6_0_ from product product0_
Process finished with exit code 1
以下是mariadb一般日志的最后几行:
86 Query insert into product (availability, brand, description, name, price, id) values (50, 'lorem', 'desc', 'Hook2', 2, 2)
86 Query commit
86 Query SET autocommit=1
86 Query SET autocommit=0
86 Query select product0_.id as id1_0_, product0_.availability as availabi2_0_, product0_.brand as brand3_0_, product0_.description as descript4_0_, product0_.name as name5_0_, product0_.price as price6_0_ from product product0_
86 Query rollback
86 Query SET autocommit=1
86 Quit
87 Quit
88 Quit
89 Quit
90 Quit
91 Quit
92 Quit
93 Quit
94 Quit
95 Quit
.count工作正常,但是,我无法从数据库中读取任何数据。我尝试在没有Entity抽象的情况下进行此操作,并且效果很好。