使用spring jpa获取数据

我还是java和spring的初学者,我已经将表存储在名为as的mysql中Offers,我试图逐行获取数据where the Status == 0,我的表看起来像:


-------------+------------+------------+------------+--------------+--------+--------+--------+--------+--------------+

| Msisdn      | Entry_Date | Start_Date | End_Date   | Service_Type | Status | Parm_1 | Parm_2 | Parm_3 | Process_Date |

+-------------+------------+------------+------------+--------------+--------+--------+--------+--------+--------------+

| 7777777777  | 2019-01-11 | 2019-02-15 | 2019-03-03 | 1            |      1 | 1      | 1      | 1      | 2019-10-15   |

| 7888888899  | 2019-01-11 | 2019-02-12 | 2019-03-03 | 1            |      0 | 1      | 1      | 1      | 2019-10-15   |

| 799999999   | 2019-01-11 | 2019-02-10 | 2019-03-03 | 1            |      0 | 1      | 1      | 1      | 2019-10-15   |

| 79111111111 | 2019-01-28 | 2019-02-27 | 2019-03-03 | 1            |      0 | 1      | 1      | 1      | 2019-10-15   |

+-------------+------------+------------+------------+--------------+--------+--------+--------+--------

当我尝试运行我的代码时它返回


org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:调用init方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.example.accessingdatajpa.Offers


优惠信息库


package com.example.accessingdatajpa;


import java.util.List;


import org.springframework.data.repository.CrudRepository;


public interface OffersRepository extends CrudRepository<Offers, String> {


    List<Offers> findByStatus(String Status);


    Offers findByMsisdn(String Msisdn);

}

MMTTMM
浏览 82回答 4
4回答

九州编程

我发现那里有很多错误:第一名添加@Id注释到msisdn@Id@GeneratedValue(strategy=GenerationType.AUTO)private String Msisdn;第二名添加@Repository注释到OffersRepository@Repositorypublic interface OffersRepository extends CrudRepository<Offers, String> {&nbsp; &nbsp; List<Offers> findByStatus(String Status);&nbsp; &nbsp; Offers findByMsisdn(String Msisdn);}第三名将类型的自动装配 bean 添加OffersRepository到您的类中,并从您的方法中AccessingDataJpaApplication删除参数OffersRepository repositorypublic CommandLineRunner demo(OffersRepository repository)@SpringBootApplicationpublic class AccessingDataJpaApplication {&nbsp; &nbsp; private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private OffersRepository repository;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(AccessingDataJpaApplication.class);&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public CommandLineRunner demo() {&nbsp; &nbsp; &nbsp; &nbsp; return (args) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // fetch by status =0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("Offers found with findByStatus('0'):");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("--------------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repository.findByStatus("0").forEach(on -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info(on.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("");&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}第四名如果你想使用CommandLineRunner,你需要实现它。您可以通过非常简单的方式完成,只需在引导类中实现即可。AccessingDataJpaApplication.java@SpringBootApplicationpublic class AccessingDataJpaApplication implements CommandLineRunner {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private OffersRepository repository;&nbsp; &nbsp; private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(AccessingDataJpaApplication.class);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run(String...args) {&nbsp; &nbsp; &nbsp; &nbsp; log.info("Offers found with findByStatus('0'):");&nbsp; &nbsp; &nbsp; &nbsp; log.info("--------------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; repository.findByStatus("0").forEach(on - >{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info(on.toString());&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; log.info("");&nbsp; &nbsp; }}

BIG阳

您缺少一个用 注释的字段@Id。每个都@Entity需要一个@Id- 这是数据库中的主键。在您的实体类上指定注释,例如:@Entity@Table(name = "OFFERS")public class Offers {   @Id   @GeneratedValue(strategy=GenerationType.AUTO)   @Column(name = "Msisdn")   private String Msisdn;   @Column(name = "Entry_Date")      private String Entry_Date;   @Column(name = "Start_Date")   private String Start_Date;   @Column(name = "End_Date")   private String End_Date;   @Column(name = "Service_Type")   private String Service_Type;   @Column(name = "Status")   private String Status;   @Column(name = "Parm_1")   private String Parm_1;   @Column(name = "Parm_2")   private String Parm_2;   @Column(name = "Parm_3")   private String Parm_3;   @Column(name = "Process_Date")   private String Process_Date;   //Setters and getters}如果您的列和表名称遵循隐式命名策略,则可以不使用注释指定表和列。@Id 注释的放置标记了持久性状态访问策略。该标识符唯一标识该表中的每一行。默认情况下,假定表的名称与实体的名称相同。要显式给出表的名称或指定有关表的其他信息,我们将使用 javax.persistence.Table 注释。逻辑名称可以由用户显式指定(例如使用 @Column 或 @Table),也可以由用户显式指定。可以由 Hibernate 通过 ImplicitNamingStrategy 契约隐式确定。

慕虎7371278

Offers 没有主键。您必须使用@Id注释主键属性喜欢@Id private&nbsp;Integer&nbsp;id;

阿波罗的战车

你需要这样做:@Id@GeneratedValue(strategy=GenerationType.AUTO)private String Msisdn;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java