I have a class (Activity) that with a field (signatureSecret) that is defined as an interface (SignatureSecret), the implementation of which (SharedConsumerSecret) comes from the Spring Security OAuth package.
When persisting instances of my class with Hibernate, I want to use a specific property of SharedConsumerSecret as the value that should be saved/loaded from the database. I can’t annotate SharedConsumerSecret, as it’s part of the Spring framework.
Is there any way to do this?
@Entity
public class Activity implements ConsumerDetails
{
@Transient
private List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
private String consumerKey;
private String consumerName;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToOne
private ActivityOwner activityOwner;
//THIS IS THE TRICKY ONE
private SignatureSecret signatureSecret;
@Size(min = 1, max = 36)
private String uuid;
...
The only way around this I can think of would be to have a one-to-one relationship of SignatureSecrets to Activities, but that seems a bit silly.
You’ll need to implement a UserType for SignatureSecret. It will need to know how to convert the object into a value (probably a String), and how to convert a String into the object.
See this example from Hibernate test suite on how to build a UserType: https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/test/java/org/hibernate/test/annotations/entity/PhoneNumberType.java