我有一个Spring数据实体(使用JPA w / Hibernate和MySQL)定义如下:
@Entity
@Table(name = "dataset")
public class Dataset {
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "guid", nullable = false)
private String guid;
@Column(name = "size", nullable = false)
private Long size;
@Column(name = "create_time", nullable = false)
private Date createTime;
@OneToOne(optional = false)
@JoinColumn(name = "created_by")
private User createdBy;
@Column(name = "active", nullable = false)
private boolean active;
@Column(name = "orig_source", nullable = false)
private String origSource;
@Column(name = "orig_source_type", nullable = false)
private String origSourceType;
@Column(name = "orig_source_org", nullable = false)
private String origSourceOrg;
@Column(name = "uri", nullable = false)
private String uri;
@Column(name = "mimetype", nullable = false)
private String mimetype;
@Column(name = "registration_state", nullable = false)
private int registrationState;
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name = "dataset_id")
@JsonManagedReference
private List<DatasetFile> datasetFiles;
我有这个实体的以下存储库:
public interface DatasetRepo extends JpaRepository<Dataset, Long> {
@Query("SELECT CASE WHEN COUNT(p) > 0 THEN 'true' ELSE 'false' END FROM Dataset p WHERE p.uri = ?1 and p.registrationState>0")
public Boolean existsByURI(String location);
@Query("SELECT a FROM Dataset a LEFT JOIN FETCH a.datasetFiles c where a.registrationState>0")
public List<Dataset> getAll(Pageable pageable);
@Query("SELECT a FROM Dataset a LEFT JOIN FETCH a.datasetFiles c WHERE a.registrationState>0")
public List<Dataset> findAll();
@Query("SELECT a FROM Dataset a LEFT JOIN FETCH a.datasetFiles c where a.guid= ?1")
public Dataset findByGuid(String guid);
}
现在 - 在一个控制器中,我正在获取一个数据集,更新其中一个属性,我希望将属性更改刷新到DB,但它永远不会。
@RequestMapping(value = "/storeDataset", method = RequestMethod.GET)
public @ResponseBody
WebServiceReturn storeDataset(
@RequestParam(value = "dsGUID", required = true) String datasetGUID,
@RequestParam(value = "stType", required = true) String stType) {
WebServiceReturn wsr = null;
logger.info("stType: '" + stType + "'");
if (!stType.equals("MongoDB") && !stType.equals("Hive") && !stType.equals("HDFS")) {
wsr = getFatalWebServiceReturn("Invalid Storage type '" + stType + "'");
} else if (stType.equals("MongoDB")) {
/* Here is where I'm reading entity from Repository */
Dataset dataset = datasetRepo.findByGuid(datasetGUID);
if (dataset != null) {
MongoLoader mongoLoader = new MongoLoader();
boolean success = mongoLoader.loadMongoDB(dataset);
logger.info("Success: " + success);
if (success) {
/* Here is where I update entity attribute value, this is never flushed to DB */
dataset.setRegistrationState(1);
}
wsr = getWebServiceReturn(success ? 0 : -1, "Successfully loaded dataset files into " + stType + " storage", "Failed to load dataset files into " + stType + " storage");
}
}
return wsr;
}
谢谢
答案 0 :(得分:0)
您需要使用@Transactional
注释请求映射的方法。
为什么呢?如果要修改内存中的对象,然后在数据库中透明地更新,则需要在活动事务中执行此操作。
请勿忘记您正在使用JPA(spring-data
正在使用JPA
),如果您希望您的实体位于managed state
,则需要有效交易
请参阅:
透明更新从中检索实体对象 数据库(无论哪种方式)都可以简单地在内存中修改 来自活跃交易:
Employee employee = em.find(Employee.class, 1); em.getTransaction().begin(); employee.setNickname("Joe the Plumber"); em.getTransaction().commit();