AuditingEntityListener is not working for the entity that extends another abstract entity in spring jpa











up vote
0
down vote

favorite












I have used the @CreatedBy, @CreatedDate, @LastModifiedBy, and @LastModifiedDate annotation on their respective fields. By using @MappedSuperclass,@EntityListeners i able to persist above columns.



But this is not working for the below case:



@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@CreatedBy
protected U createdBy;
@CreatedDate
@Temporal(TIMESTAMP)
protected Date creationDate;
@LastModifiedBy
protected U lastModifiedBy;
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedDate;
}

@Entity
@Table(name = "tabel1")
@PrimaryKeyJoinColumn(name = "ID")
class A extends B {

@Column(name = "NAME1", nullable = false)
private String name1;
@Column(name = "CONTENT1", nullable = false)
private String content1;
}

@Entity
@Table(name = "tabel2")
public abstract class B extends Auditable{
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private int id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "CONTENT", nullable = false)
private String content;
}


AuditorAwareImpl.java



public class AuditorAwareImpl implements AuditorAware<String>
{
@Override
public Optional<String> getCurrentAuditor()
{
return Optional.ofNullable("Saravanan");

}

}


JpaAuditConfiguration.java



@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditConfiguration
{
@Bean
public AuditorAware<String> auditorProvider()
{

return new AuditorAwareImpl();
}
}


In the case, Entity B is populated with audit columns. But Entity A is not. Is there a way to populate Entity A or did i missed anything here..??










share|improve this question
























  • Is class A defined as entity?
    – Periklis Douvitsas
    Nov 19 at 15:10










  • yes, i edited the code also
    – Saravanan
    Nov 20 at 4:42















up vote
0
down vote

favorite












I have used the @CreatedBy, @CreatedDate, @LastModifiedBy, and @LastModifiedDate annotation on their respective fields. By using @MappedSuperclass,@EntityListeners i able to persist above columns.



But this is not working for the below case:



@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@CreatedBy
protected U createdBy;
@CreatedDate
@Temporal(TIMESTAMP)
protected Date creationDate;
@LastModifiedBy
protected U lastModifiedBy;
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedDate;
}

@Entity
@Table(name = "tabel1")
@PrimaryKeyJoinColumn(name = "ID")
class A extends B {

@Column(name = "NAME1", nullable = false)
private String name1;
@Column(name = "CONTENT1", nullable = false)
private String content1;
}

@Entity
@Table(name = "tabel2")
public abstract class B extends Auditable{
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private int id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "CONTENT", nullable = false)
private String content;
}


AuditorAwareImpl.java



public class AuditorAwareImpl implements AuditorAware<String>
{
@Override
public Optional<String> getCurrentAuditor()
{
return Optional.ofNullable("Saravanan");

}

}


JpaAuditConfiguration.java



@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditConfiguration
{
@Bean
public AuditorAware<String> auditorProvider()
{

return new AuditorAwareImpl();
}
}


In the case, Entity B is populated with audit columns. But Entity A is not. Is there a way to populate Entity A or did i missed anything here..??










share|improve this question
























  • Is class A defined as entity?
    – Periklis Douvitsas
    Nov 19 at 15:10










  • yes, i edited the code also
    – Saravanan
    Nov 20 at 4:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have used the @CreatedBy, @CreatedDate, @LastModifiedBy, and @LastModifiedDate annotation on their respective fields. By using @MappedSuperclass,@EntityListeners i able to persist above columns.



But this is not working for the below case:



@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@CreatedBy
protected U createdBy;
@CreatedDate
@Temporal(TIMESTAMP)
protected Date creationDate;
@LastModifiedBy
protected U lastModifiedBy;
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedDate;
}

@Entity
@Table(name = "tabel1")
@PrimaryKeyJoinColumn(name = "ID")
class A extends B {

@Column(name = "NAME1", nullable = false)
private String name1;
@Column(name = "CONTENT1", nullable = false)
private String content1;
}

@Entity
@Table(name = "tabel2")
public abstract class B extends Auditable{
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private int id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "CONTENT", nullable = false)
private String content;
}


AuditorAwareImpl.java



public class AuditorAwareImpl implements AuditorAware<String>
{
@Override
public Optional<String> getCurrentAuditor()
{
return Optional.ofNullable("Saravanan");

}

}


JpaAuditConfiguration.java



@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditConfiguration
{
@Bean
public AuditorAware<String> auditorProvider()
{

return new AuditorAwareImpl();
}
}


In the case, Entity B is populated with audit columns. But Entity A is not. Is there a way to populate Entity A or did i missed anything here..??










share|improve this question















I have used the @CreatedBy, @CreatedDate, @LastModifiedBy, and @LastModifiedDate annotation on their respective fields. By using @MappedSuperclass,@EntityListeners i able to persist above columns.



But this is not working for the below case:



@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@CreatedBy
protected U createdBy;
@CreatedDate
@Temporal(TIMESTAMP)
protected Date creationDate;
@LastModifiedBy
protected U lastModifiedBy;
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedDate;
}

@Entity
@Table(name = "tabel1")
@PrimaryKeyJoinColumn(name = "ID")
class A extends B {

@Column(name = "NAME1", nullable = false)
private String name1;
@Column(name = "CONTENT1", nullable = false)
private String content1;
}

@Entity
@Table(name = "tabel2")
public abstract class B extends Auditable{
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private int id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "CONTENT", nullable = false)
private String content;
}


AuditorAwareImpl.java



public class AuditorAwareImpl implements AuditorAware<String>
{
@Override
public Optional<String> getCurrentAuditor()
{
return Optional.ofNullable("Saravanan");

}

}


JpaAuditConfiguration.java



@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditConfiguration
{
@Bean
public AuditorAware<String> auditorProvider()
{

return new AuditorAwareImpl();
}
}


In the case, Entity B is populated with audit columns. But Entity A is not. Is there a way to populate Entity A or did i missed anything here..??







spring spring-mvc spring-boot spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 13:09

























asked Nov 19 at 13:24









Saravanan

4,88935118196




4,88935118196












  • Is class A defined as entity?
    – Periklis Douvitsas
    Nov 19 at 15:10










  • yes, i edited the code also
    – Saravanan
    Nov 20 at 4:42


















  • Is class A defined as entity?
    – Periklis Douvitsas
    Nov 19 at 15:10










  • yes, i edited the code also
    – Saravanan
    Nov 20 at 4:42
















Is class A defined as entity?
– Periklis Douvitsas
Nov 19 at 15:10




Is class A defined as entity?
– Periklis Douvitsas
Nov 19 at 15:10












yes, i edited the code also
– Saravanan
Nov 20 at 4:42




yes, i edited the code also
– Saravanan
Nov 20 at 4:42












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I added @Entity annotation to your classes:



@Entity
public class A extends B {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;

}

@Entity
public class B extends Auditable<String> {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;
}


Persistence config class (for Spring Boot):



@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}


Everything works perfectly!






share|improve this answer























  • Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
    – Saravanan
    Nov 20 at 4:52










  • I have published my source code here. Hope it might be useful for you.
    – huytmb
    Nov 20 at 7:35










  • when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
    – Saravanan
    Nov 21 at 9:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375607%2fauditingentitylistener-is-not-working-for-the-entity-that-extends-another-abstra%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I added @Entity annotation to your classes:



@Entity
public class A extends B {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;

}

@Entity
public class B extends Auditable<String> {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;
}


Persistence config class (for Spring Boot):



@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}


Everything works perfectly!






share|improve this answer























  • Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
    – Saravanan
    Nov 20 at 4:52










  • I have published my source code here. Hope it might be useful for you.
    – huytmb
    Nov 20 at 7:35










  • when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
    – Saravanan
    Nov 21 at 9:44















up vote
0
down vote













I added @Entity annotation to your classes:



@Entity
public class A extends B {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;

}

@Entity
public class B extends Auditable<String> {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;
}


Persistence config class (for Spring Boot):



@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}


Everything works perfectly!






share|improve this answer























  • Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
    – Saravanan
    Nov 20 at 4:52










  • I have published my source code here. Hope it might be useful for you.
    – huytmb
    Nov 20 at 7:35










  • when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
    – Saravanan
    Nov 21 at 9:44













up vote
0
down vote










up vote
0
down vote









I added @Entity annotation to your classes:



@Entity
public class A extends B {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;

}

@Entity
public class B extends Auditable<String> {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;
}


Persistence config class (for Spring Boot):



@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}


Everything works perfectly!






share|improve this answer














I added @Entity annotation to your classes:



@Entity
public class A extends B {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;

}

@Entity
public class B extends Auditable<String> {

@Id
@GeneratedValue
private Integer id;

private String name;
private String content;
}


Persistence config class (for Spring Boot):



@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}


Everything works perfectly!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 7:17

























answered Nov 19 at 15:26









huytmb

824




824












  • Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
    – Saravanan
    Nov 20 at 4:52










  • I have published my source code here. Hope it might be useful for you.
    – huytmb
    Nov 20 at 7:35










  • when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
    – Saravanan
    Nov 21 at 9:44


















  • Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
    – Saravanan
    Nov 20 at 4:52










  • I have published my source code here. Hope it might be useful for you.
    – huytmb
    Nov 20 at 7:35










  • when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
    – Saravanan
    Nov 21 at 9:44
















Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
– Saravanan
Nov 20 at 4:52




Thanks for your reply.. But for me A entity is not populating with the audit fields..I have updated other classes also.. could you pls verify it
– Saravanan
Nov 20 at 4:52












I have published my source code here. Hope it might be useful for you.
– huytmb
Nov 20 at 7:35




I have published my source code here. Hope it might be useful for you.
– huytmb
Nov 20 at 7:35












when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
– Saravanan
Nov 21 at 9:44




when i looked at my code B entity declared as abstract here... I have modified my code above. Could you please have a look at that huymb..?
– Saravanan
Nov 21 at 9:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375607%2fauditingentitylistener-is-not-working-for-the-entity-that-extends-another-abstra%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin