为什么我的实体列表没有使用jpa持久化到h2数据库

tvmytwxo  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(270)

我有一个h2 dao类,它定义了将数据插入h2数据库的持久性方法。当我从验证器类(h2impl.insertmydata(batchid,promolist))调用我的方法时,似乎什么都没有发生,也没有数据持久化到我的数据库中。我从属性文件本机调用查询,并从持久性bean设置参数。
下面是我的持久性bean、dao、验证器和查询的query.properties文件。所有这些都设置在我的h2数据库中:
持久性bean类:

@Entity
public class InsertPromoData {

    @Id
    @Column(name="row_num")
    String rowNumber;

    @Column(name="BATCH_ID")
    String batchID;

    @Column(name="ITEM")
    String item;

    @Column(name="LOC")
    String loc;

    @Column(name="WK_START")
    String wkStart;

    @Column(name="NUM_OF_WEEKS")
    String numberOfWeeks;

    @Column(name="TYPE")
    String type;

    @Column(name="FCSTID")
    String fcstId;

    @Column(name="QTY")
    String qty;

    @Column(name="INSERT_TS")
    String insertTS;

    public String getRowNumber() {
        return rowNumber;
    }

    public void setRowNumber(String rowNumber) {
        this.rowNumber = rowNumber;
    }

    public String getBatchID() {
        return batchID;
    }

    public void setBatchID(String batchID) {
        this.batchID = batchID;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public String getWkStart() {
        return wkStart;
    }

    public void setWkStart(String wkStart) {
        this.wkStart = wkStart;
    }

    public String getNumberOfWeeks() {
        return numberOfWeeks;
    }

    public void setNumberOfWeeks(String numberOfWeeks) {
        this.numberOfWeeks = numberOfWeeks;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFcstId() {
        return fcstId;
    }

    public void setFcstId(String fcstId) {
        this.fcstId = fcstId;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }

    public String getInsertTS() {
        return insertTS;
    }

    public void setInsertTS(String insertTS) {
        this.insertTS = insertTS;
    }
}

h2impl dao类:

@Configuration // Needs to be a configuration
@PropertySources({ @PropertySource("classpath:sql/query.properties") })
public class H2Impl {
    private static Logger logger = LogManager.getLogger(H2Impl.class);
    private static EntityManagerFactory emf;
    private static Environment env;

    public static void setMyConfig(EntityManagerFactory emf, Environment env) {
        H2Impl.emf = emf;
        H2Impl.env = env;

    }

  public static void insertPromoData(String batchId, List<InsertPromoData> insertData) {
      //public static List<InsertPromoData> insertPromoData(String batchId, List<InsertPromoData> insertData) {
      logger.debug("Execution of method insertPromoData in Dao started");

      List<InsertPromoData> insertPromo = new ArrayList<>();
      EntityManager em = emf.createEntityManager();
      EntityTransaction et = em.getTransaction();
        //logic to select the max_row number before executing the insert query
        //counter to see if after 1000 the mod is x % 1000 = 0 then commit
      try {
          et.begin();
          for (InsertPromoData promoData : insertData) {

              Query query = em.createNativeQuery(env.getProperty("insertPromoUploadData"));
              query.setParameter("item", promoData.getItem());
              query.setParameter("loc", promoData.getLoc());
              query.setParameter("wkStart", promoData.getWkStart());
              query.setParameter("numberOfWeeks", promoData.getNumberOfWeeks());            
              query.setParameter("type", promoData.getType());
              query.setParameter("fcstId", promoData.getFcstId());
              query.setParameter("qty", Double.valueOf(promoData.getQty()));
              em.persist(promoData);
              em.flush();
          }
          //insertPromo = insertData;
          et.commit();
      }
      catch(Exception e) {
          logger.error("Exception in beginning transaction");
          e.printStackTrace();
      }
      finally {
          em.clear();
          em.close();
      }

      logger.debug("Execution of method insertPromoData in Dao ended");
      //return insertPromo;
  }
}

validator类(现有的工作类,但我的持久性逻辑从下面开始):

promo.forEach(record -> {

        if (record.getErrorList().size() == 0) {
            rowsSuccessful++;
            record.setNumber_Of_Weeks(record.getNumber_Of_Weeks());
            record.setForecast_ID(record.getForecast_ID());
            record.setType(record.getType());
            record.setUnits(record.getUnits());
            record.setWeek_Start_Date(record.getWeek_Start_Date());
            for (InsertPromoData insertPromoData : insertPromos) {

                insertPromoData.setItem(record.getItem());
                insertPromoData.setLoc(record.getLoc());
                insertPromoData.setWkStart(record.getWeek_Start_Date());
                insertPromoData.setNumberOfWeeks(record.getNumber_Of_Weeks());
                insertPromoData.setType(record.getType());          
                insertPromoData.setFcstId(record.getForecast_ID());
                insertPromoData.setQty(record.getUnits());
            }
        );

        H2Impl.insertPromoData(batchID, insertPromos);

我的query.properties条目:

insertPromoUploadData =INSERT INTO TEST.U_USER_PROMO (BATCH_ID,ITEM,LOC,WK_START,NUM_OF_WEEKS,TYPE,FCSTID,QTY,INSERT_TS) VALUES (:batchId,:item,:loc,:wkStart,:numberOfWeeks,:type,:fcstId,:qty,CURRENT_TIMESTAMP)

当我调用持久性方法时,为什么什么都没有发生?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题