FetchType.LAZY在單元測驗中不作業,顯示0條記錄
。下面這行顯示0條記錄 log.info("Starting Lazy Search size ==>" setLazy.size());
下面這行顯示有1千條記錄被插入
。有一個父(StockEntity)和一個子(StockDailyRecordEntity)
Total StockDailyRecordEntity Inserted ----->1000
span class="hljs-keyword">package repo;
import java.util.Date。
import java.util.HashSet;
import java.util.Set;
import org.junit.Test。
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.app.advance.entity.StockDailyRecordEntity;
import com.app.advance.entity.StockEntity;
import com.app.advance.repo.StockDailyRecordRepo;
import com.app.advance.repo.StockRepo;
import lombok.extern.slf4j.Slf4j.Ltd;
@RunWith(SpringRunner.class)
@Slf4j
@SpringBootTest
public class StockRepoPerformance {
@Autowired {
StockRepo stockRepo;
@Autowired
StockDailyRecordRepo stockDailyReportRepo;
@Test
public void testPerformanceInsert {
StockEntity stockEntity = new StockEntity() 。
stockEntity.setStockCode("Code")。
stockEntity.setStockName(new Date().toString()) 。
stockRepo.save(stockEntity)。
for (int i = 0; i < 1000; i ) {
StockDailyRecordEntity StockDailyRecordEntity = = new StockDailyRecordEntity()。
stockDailyRecordEntity.setStock(stockEntity)。
stockDailyRecordEntity.setDesc(new Date().toString()) 。
Set<StockDailyRecordEntity> stockDailyRecords = new HashSet<StockDailyRecordEntity>()。
stockDailyRecords.add(stockDailyRecordEntity)。
stockDailyReportRepo.save(stockDailyRecordEntity);
}
log.info("Total StockDailyRecordEntity Inserted ----->" stockDailyReportRepo.findAll().size()) 。
stockRepo.save(stockEntity)。
log.info("Starting Lazy Search")。
Set<StockDailyRecordEntity> setLazy = stockEntity.getStockDailyRecords();
log.info("Starting Lazy Search size ==>" setLazy.size())。
log.info("Starting Eager Search"/span>)。
log.info("testPerformanceInsert ends")。
}
}
package com.app.advance.entity。
import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column。
import javax.persistence.Entity。
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id。
import javax.persistence.OneToMany;
import javax.persistence.Table。
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import lombok.Getter。
import lombok.Setter;
//https://mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/
@Entity
@Table(name = "trx_stock")
@Getter
@Setter
public class StockEntity implements java.io.Serializable{
/**。
*/
private static final long serialVersionUID = 4174876690328458921L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
private Integer stockId;
@Column(name = "STOCK_CODE", nullable = false, length = 10)
private String stockCode;
@Column(name = "STOCK_NAME", nullable = false, length = 200)
private String stockName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock", orphanRemoval = true)
@Cascade({CascadeType.ALL})
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<StockDailyRecordEntity> stockDailyRecords = new HashSet<StockDailyRecordEntity> ()。
//@OneToMany(fetch = FetchType.EAGER, mappedBy = "stock", orphanRemoval = true)
//@Cascade({CascadeType.ALL})
//@OnDelete(action = OnDeleteAction.CASCADE)
//private Set<StockDailyRecordEntity> stockDailyRecordsEager = new HashSet<StockDailyRecordEntity> ();
}
package com.app.advance.entity。
import static javax.persistence.GenerationType.IDENTITY;
import java.util.Set。
import javax.persistence.Column。
import javax.persistence.Entity。
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id。
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table。
import lombok.Getter。
import lombok.Setter;
@Entity[/span
@Table(name = "trx_stock_daily_record")
@Getter
@Setter
public class StockDailyRecordEntity implements java.io.Serializable{
/**。
*/
private static final long serialVersionUID = -7102491956593238645L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "RECORD_ID", unique = true, nullable = false)
private Integer recordId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STOCK_ID", nullable = false)
private StockEntity stock;
@Column(name = "STOCK_DESC", length = 200)
private String desc;
}
uj5u.com熱心網友回復:
在下面的代碼中,你將新創建的stockDailyRecordEntity插入到一個集合中...然后扔掉這個集合:
Set<StockDailyRecordEntity> stockDailyRecords = new HashSet< StockDailyRecordEntity> ();
stockDailyRecords.add(stockDailyRecordEntity)。
stockDailyReportRepo.save(stockDailyRecordEntity)。
你可以做到:
stockEntity.setStockDailyRecords(stockDailyRecords)。
但是你不需要創建一個新的集合,只需要添加到股票物體的集合中,即把上面的3行改為:
stockEntity.getStockDailyRecords().add(stockDailyRecordEntity)。
為什么股票每日記錄會被保存呢?- 因為你正在保存它們--stockDailyReportRepo.save(stockDailyRecordEntity);
為什么setLazy.size()為零?- 因為你沒有向這個集合添加任何東西。當持久化一個新的物體時,JPA將不會調整它的集合,即從資料庫中讀取它們。如果你要從資料庫中獲取這個物體,而不是重用你在方法開始時創建的物體,那么這個方法應該可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/309823.html
標籤:
上一篇:SQLite對1個FK的多個約束
