简体   繁体   中英

Hibernate and spring data repository, object incomplete after save or update

After saving or updating an object of this entity through a spring data repository (which extends CrudRepository):

@Entity
@Table(name="finance_data")
public class FinanceData implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="asset_type", nullable=false)
private AssetType assetType;

It only returns the Id for the assetType property. This is forcing us to execute a findOne after the update to get the full information.

How can I tell to hibernate to return the whole object?

It only returns the Id for the assetType property.

This is because all the other fields are set to null by your own action (as described in the comments):

When I try to save a new FinanceData record, I only set the reference id for property AssetType because they exist before the FinanceData is recorded.

Instead of just setting the Id and thereby overwriting all other fields, load the AssetType by Id and use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM