简体   繁体   中英

'Basic' attribute type should not be 'Mapped Superclass'

I'm using Spring Data JPA in my project. I would like to keep track of @CreatedBy and @LastModifiedBy when an entity is saved or updated to know which user does that activity. But the issue I am facing is I have 2 different types of users. Those are SysUser and RefUser . Both of the user types have permission to do CRUD operations in any entity.

And another thing is I have seen the most examples persisting @CreatedBy and @LastModifiedBy as String fields. But the issue is when retrieving data from the database they are retrieving as User Ids (String values). I need to store user Ids as Strings, but when retrieving it should come the user's all details like user's id, name, gender etc.

public class Record extends Auditable<String> implements Serializable {}

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<T> {

    @CreatedBy
    protected T createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    protected Date createdDate;

    @LastModifiedBy
    protected T lastModifiedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    protected Date lastModifiedDate;
}

Also I have a common class annotated with @MappedSuperClass that extended by SysUser and RefUser . But when I am trying to add that common class as a filed like the following it says

'Basic' attribute type should not be 'Mapped Superclass'

private CommonEntity crUsr;
private CommonEntity upUsr;

So how can I achieve this? Anybody can help me? Thanks in advance.

Well, You can do one thing here. Create a common class named User put all the common properties from SysUser and RefUser in that class. Then you can extend that in both the class and keep only distinct fields in the Child classes. This way any user do the operation will belong to User Class.

After that you can do something like this in your Class:

public class Record extends Auditable<String> implements Serializable {}

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<User> {

@CreatedBy
protected User createdBy;

@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
protected Date createdDate;

@LastModifiedBy
protected T lastModifiedBy;

@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
protected Date lastModifiedDate;
}

This should solve your problem.

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