Add support for @Immutable attribute types
Description
Activity
Show:
Vlad Mihalcea May 18, 2016 at 2:37 PM
The PR was applied on master. Thanks for your contribution.
Fixed
Details
Details
Assignee
Vlad Mihalcea
Vlad MihalceaReporter
Vlad Mihalcea
Vlad MihalceaComponents
Fix versions
Priority
Created April 28, 2016 at 9:39 AM
Updated June 2, 2016 at 3:50 AM
Resolved May 18, 2016 at 2:37 PM
Currently,
@Immutable
works for entities and collections, as indicated by ANN-542.By adding support for attribute types, we can benefit from:
lower memory consumption (no need to create copies for checking if entity is dirty or to put/read it from cache) - especially in cases where entity contains JSON documents
faster flush operations
An immutable attribute type looks like this:
@Immutable public class Caption { private String text; public Caption(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } @Override public boolean equals(Object o) { if ( this == o ) { return true; } if ( o == null || getClass() != o.getClass() ) { return false; } Caption caption = (Caption) o; return text != null ? text.equals( caption.text ) : caption.text == null; } @Override public int hashCode() { return text != null ? text.hashCode() : 0; } }
And it is used as follows:
@Entity @SuppressWarnings("serial") public class Photo implements Serializable { private Integer id; private String name; private Exif metadata; private Caption caption; @Id @GeneratedValue public Integer getId() { return id; } public String getName() { return name; } public void setId(Integer integer) { id = integer; } public void setName(String string) { name = string; } public Exif getMetadata() { return metadata; } public void setMetadata(Exif metadata) { this.metadata = metadata; } @Convert(converter = CaptionConverter.class) public Caption getCaption() { return caption; } public void setCaption(Caption caption) { this.caption = caption; } }
The
@Immutable
annotation still cannot be applied on fields directly, but it can work on an attribute type as illustrated above.