I am using a `data class` for the Entity but, unle...
# spring
d
I am using a
data class
for the Entity but, unless I'm missing something, I'm not sure that should matter. Has anybody successfully used a delegate in their entities?
t
I did it, but not with data classes. afair, I had problems with data classes, delegation and jpa and that forced me to read about jp and why data classes should not be used there. other than that, with regular delegation it worked
beware, I had to annotate the delegate with
@Embeddable
Copy code
interface Auditable {
	var createdBy: Int?
	var createdAt: LocalDateTime?
	var modifiedBy: Int?
	var modifiedAt: LocalDateTime?
}

@Embeddable
data class BaseAuditable(
	@Column(name = "modified_at")
	@LastModifiedDate
	override var modifiedAt: LocalDateTime? = null,
	@Column(name = "modified_by")
	@LastModifiedBy
	override var modifiedBy: Int? = null,
	@Column(name = "created_at")
	@CreatedDate
	override var createdAt: LocalDateTime? = null,
	@Column(name = "created_by")
	@CreatedBy
	override var createdBy: Int? = null
) : Auditable

@Entity(name = "plan")
@EntityListeners(AuditingEntityListener::class)
class Plan(
	@Column(name = "file_uid")
	var fileUid: String,
	@Column(name = "file_name")
	var fileName: String,
	@Column(name = "project_uid")
	var projectUid: String,
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	var id: Int? = null,
	@Embedded
	val auditable: BaseAuditable = BaseAuditable()
) : Auditable by auditable
d
Interesting, most of the articles put the annotations on the interface, yours is on the implementing class. That might be why mine isn't working.
@thanksforallthefish Is there any configuration you need to set up to get this working? It looks like my setup is not picking up the Embedded auditable.
I got it working. Thanks for the help!
b
What was it?
d
I set the type of
auditable
to
Auditable
instead of
BaseAuditable
Then this works perfectly. The other issue I had (likely due to accidentally reverting a change) was that I was using the Hibernate api for created and updated dates instead bof the spring api.