spring-boot-starter-data-jpa插入查询不会将数据保存在数据库中

时间:2019-04-18 05:40:42

标签: spring-boot

我正在使用spring-boot-starter-data-jpa。尝试在数据库中插入数据时,该实用程序会执行而不会出现任何错误,但是数据不会保留在数据库中。 我还可以在控制台上看到插入查询 我不确定这里到底出了什么问题。

POM.XML

<dependencies>    
     <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
     </dependency>

     <!-- Spring data JPA -->
     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
          <exclusions>
            <exclusion>
                 <groupId>org.apache.tomcat</groupId>
                 <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
          </exclusions>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>

      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.15</version>
      </dependency>

 </dependencies>

主班

 @SpringBootApplication
 public class Application implements CommandLineRunner {

    @Autowired
    DataSource dataSource;

    @Autowired
    ElementDetailsRepository elementDetailsDAO;

    @Autowired
    ElementDetailsEntity elementDetailsEntity;

    @Autowired
    ElementDetailsPK elementDetailsPK;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Transactional(propagation = Propagation.NESTED)
    @Override
    public void run(String... args) throws Exception {
      for (ExcelPojo excelPojo : listOfExcelElements) {
       if (excelPojo.getRecordType().equalsIgnoreCase("Item")) {
       if (excelPojo.getMarket().equalsIgnoreCase("UK")) {
       Integer elementID = elementDetailsDAO.findDNAElementId(excelPojo.getMarket(), 992,
                                    excelPojo.getGenesisID(), "EN");
                            System.out.println("UK ITEM..." + elementID);

        int flag = elementDetailsDAO.createPIMIDAttributeValue(excelPojo.getMarket(), 1914, excelPojo.getPimID(),elementID, "EN", getCurrentTime(), getUser());
  }
  }

存储库

    @Repository
    public interface ElementDetailsRepository extends JpaRepository<ElementDetailsEntity,Integer> {

        @Query(value = QueryConstants.findElementId, nativeQuery = true)
        public Integer findDNAElementId(@Param("country_code") String country_code, @Param("config_id") int config_id,
                @Param("import_id") String import_id,@Param("language_code")String language_code);

        @Modifying
        @Query(value = QueryConstants.createPIMIDAttributeValue, nativeQuery = true)
        public int createPIMIDAttributeValue(@Param("countrycode") String countrycode, @Param("configid") int configid,
                @Param("pimid") String attribute_value, @Param("elementid") int elementId,@Param ("languageCode") String language_code,@Param("date") String date,@Param("user") String user);
    }

实体

@Service
@Entity
@Table(name = "element_details")
public class ElementDetailsEntity extends BaseDataObject {

 private static final long serialVersionUID = -8033261173844397824L;

 @EmbeddedId
 private ElementDetailsPK elementDetailsPK;

 @Column(name = "status_id")
 private Integer statusId;

 @Column(name = "attribute_value")
 private String attributeValue;

 @Column(name = "is_dirty")
 private Boolean isDirty;

}

Application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/wxb_prod
    spring.datasource.username=root
    spring.datasource.password=admin
    spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto = none
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    logging.level.org.hibernate.SQL=TRACE
    spring.datasource.hikari.connectionTimeout=20000
    spring.datasource.hikari.maximumPoolSize=5

1 个答案:

答案 0 :(得分:0)

尝试在主类中使用注释@EnableJpaRepositories