18

I use Hibernate Envers:

@Entity 
@Table(name = "user")
@Audited
class User()
{
    private String id;
    @Formula("(SELECT name FROM other c where c.id = id)")
    private Integer name;
}

it throws:

[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.envers.configuration.internal.metadata.FormulaNotSupportedException: Formula mappings (aside from @DiscriminatorValue) are currently not supported

How to calculate entity attributes with @Formula and Hibernate Envers?

FYI When I remove Hibernate Envers it works properly.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51

1 Answers1

26

The problem is that you're asking Envers to audit a @Formula annotated column, which presently isn't supported. I have opened JIRA HHH-11785 for the sole purpose of looking into this farther.

However, you should be able to annotate the formula field with @NotAudited and Envers should integrate just fine with that configuration. The real issue is that it fails when it finds the formula-based field's history is to be tracked.

As an example:

@Entity
@Audited
class User {
  @Formula("SELECT name FROM Other ...")
  @NotAudited
  private String name;
  // other attributes
}
Naros
  • 19,928
  • 3
  • 41
  • 71
  • your code give me: h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'user0_.name' in 'field list' nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] – Kamil Nękanowicz Jun 08 '17 at 07:43
  • Did you verify your formula is valid? The only code I suggested to you was to mark the field as not being audited so that Envers proceeded past your failure point. I'd need more details to help further but it likely sounds like some invalid query being executed. – Naros Jun 08 '17 at 14:17
  • your example does not work, I added bracket like this:@Formula("(SELECT name FROM Other ...)") and now it works – Kamil Nękanowicz Jun 19 '17 at 11:48
  • 1
    If you have to wrap the statement in `(` and `)` then that is a bug. You shouldn't have to wrap the `@Formula` annotations. I'll add a note in the JIRA regarding looking into this too. – Naros Jun 19 '17 at 13:06
  • If you could add the stacktrace of the error without the parenthesis to a comment in the JIRA mentioned, that would be quite helpful. – Naros Jun 19 '17 at 13:09
  • I got jdbc.exceptions.jdbc4.MySQLSyntaxErrorException You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT r.id FROM user r WHERE r.id=user0_.id as ' at line 1 – Kamil Nękanowicz Jun 19 '17 at 13:18
  • My hope was you'd add the full stack trace to the JIRA issue, not this post. – Naros Jun 19 '17 at 13:59
  • it does not depend on parenthesis in the oracle database, and it fails on application startup. – Mohammad Mirzaeyan Jun 27 '21 at 04:20