I've been having trouble with saving embedded collections. I've come up with this crazy solution because I wanted my lazily loaded models to sort based on fields in resources.
I have three tables:
- assets (id)
- assets_resources (asset_id,resource_id,primary_image)
- resources (id,logical_name) 开发者_C百科
I have defined the tables with hibernate annotations like so: Assets.java:
@Entity
@Table(name = "assets")
public class Asset implements java.io.Serializable {
@OneToMany
@Cascade(CascadeType.ALL)
@JoinTable(
name="assets_resources"
, joinColumns=@JoinColumn(name="asset_id")
, inverseJoinColumns=@JoinColumn(name="resource_id")
)
@MapKeyColumn(name="asset_id")
@OrderBy("logical_name")
private Map<AssetResource, Resource> resources;
public Map<AssetResource, Resource > getResources() {return resources;}
public void setResources(Map<AssetResource, Resource> resources) {this.resources = resources;}
}
assetResource.java
@Embeddable
public class AssetResource {
private Boolean primary_image;
public Boolean getPrimaryImage() {return primary_image;}
public void setPrimaryImage(Boolean primary_image) {this.primary_image = primary_image;}
private Long id;
public Long getId() {return id;}
}
resource.java
@Entity
@Table(name="resources")
public class Resource implements java.io.Serializable {
...
public String logical_name;
public String getLogicalName() {return logical_name;}
public void setLogicalName(String logical_name) { this.logical_name = logical_name;}
}
I can iterate though the lazily loaded maps in my controllers but I can't seem to save/update embedded fields. specifically, I can't update the primary_image field.
This link demonstrates what I was trying to do.
Are you able to save the attribute value in database. Because i think you won't be able to save the value as u have not mapped it with some column in the database. Try removind Emmbedded and then add the table and column attributes..
精彩评论