alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (AuditEntityNameRegister.java)

This example Hibernate source code file (AuditEntityNameRegister.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

auditentitynameregister, auditentitynameregister, hashset, mappingexception, mappingexception, set, string, string, the, the, util

The Hibernate AuditEntityNameRegister.java source code

package org.hibernate.envers.configuration.metadata;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.MappingException;

/**
 * A register of all audit entity names used so far.
 * @author Adam Warski (adam at warski dot org)
 */
public class AuditEntityNameRegister {
    private final Set<String> auditEntityNames = new HashSet();

    /**
     * @param auditEntityName Name of the audit entity.
     * @return True if the given audit entity name is already used.
     */
    private boolean check(String auditEntityName) {
        return auditEntityNames.contains(auditEntityName);
    }

    /**
     * Register an audit entity name. If the name is already registered, an exception is thrown.
     * @param auditEntityName Name of the audit entity.
     */
    public void register(String auditEntityName) {
        if (auditEntityNames.contains(auditEntityName)) {
            throw new MappingException("The audit entity name '" + auditEntityName + "' is already registered.");
        }
        
        auditEntityNames.add(auditEntityName);
    }

    /**
     * Creates a unique (not yet registered) audit entity name by appending consecutive numbers to the base
     * name. If the base name is not yet used, it is returned unmodified.
     * @param baseAuditEntityName The base entity name.
     * @return 
     */
    public String createUnique(final String baseAuditEntityName) {
        String auditEntityName = baseAuditEntityName;
        int count = 1;
        while (check(auditEntityName)) {
            auditEntityName = baseAuditEntityName + count++;
        }

        return auditEntityName;
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate AuditEntityNameRegister.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.