Search code examples
javahibernatehibernate-envers

Envers adding user information


I got a problem with Envers. At first, here are my two hibernate classes:

child.java:

@Entity
@Audited
public class child {
    @Id
    private int id;
    private String text;
    @ManyToOne
    @JoinColumn(name="child")
    private testclass parent;
    public int getId() {return id;}
    public void setId(int id) {this.id = id;}
    public String getText() {return text;}
    public void setText(String text) {this.text = text;}
    public testclass getParent() {return parent;}
    public void setParent(testclass parent) {this.parent = parent;}
}

testclass.java:

@Entity
@Audited
public class testclass {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_template")
    @SequenceGenerator(name="seq_template", sequenceName="tester_seq")
    private int id;

    private String text;

    @OneToMany(mappedBy="parent")
    private Set<child> childs;

    public int getId() {return id;}
    public void setId(int id) {this.id = id;}
    public String getText() {return text;}
    public void setText(String text) {this.text = text;}
    public Set<child> getChilds() {return childs;}
    public void setChilds(Set<child> childs) {this.childs = childs;}
}

Both classes are working fine and the audit information, like timestamp is stored well.

I want to extend now the information with the audit information username. For that I created these configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="ch.triwdata.envers.extensions">
    <class name="ExtendedRevisionEntity" table="revinfo">
        <id name="id" type="int">
            <column name="rev" not-null="true"/>
            <generator class="native"/>
        </id>
        <property name="timestamp" type="long" column="timestamp" />
        <property name="username" type="string" not-null="true"/>
    </class>
</hibernate-mapping>

there is of course also a configuration file with hibernate-configuration, but I don't think that this is important here.

Could anybody see my failure, why the userinformation are not stored?

Best regards Björn

PS: Maybe this xml is missing? I've added it of course since begining:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.password">****</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create-drop</property>

        <mapping class="testclass"/>
        <mapping class="child"/>
    </session-factory>
</hibernate-configuration>

Solution

  • I got it! :D

    The failure was here:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property>
            <property name="hibernate.connection.username">twd_genmeta</property>
    <!--         <property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> -->
            <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
            <property name="connection.password">Deliaa</property>
            <property name="connection.pool_size">1</property>
            <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">create-drop</property>
    
            <mapping class="ch.triwdata.envers.classes.testclass"/>
            <mapping class="ch.triwdata.envers.classes.child"/>
            <mapping resource="hibernate.hbm.xml"/> <---- this entry is missing
        </session-factory>
    </hibernate-configuration>
    

    Thanks a lot for your reply!

    Best regards Björn