|
Hibernate example source code file (RevisionInfoConfiguration.java)
The Hibernate RevisionInfoConfiguration.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.envers.configuration; import java.util.Date; import java.util.Iterator; import java.util.Set; import javax.persistence.Column; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.hibernate.MappingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.cfg.Configuration; import org.hibernate.envers.Audited; import org.hibernate.envers.DefaultRevisionEntity; import org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity; import org.hibernate.envers.ModifiedEntityNames; import org.hibernate.envers.RevisionEntity; import org.hibernate.envers.RevisionListener; import org.hibernate.envers.RevisionNumber; import org.hibernate.envers.RevisionTimestamp; import org.hibernate.envers.configuration.metadata.AuditTableData; import org.hibernate.envers.configuration.metadata.MetadataTools; import org.hibernate.envers.entities.PropertyData; import org.hibernate.envers.revisioninfo.DefaultRevisionInfoGenerator; import org.hibernate.envers.revisioninfo.DefaultTrackingModifiedEntitiesRevisionInfoGenerator; import org.hibernate.envers.revisioninfo.ModifiedEntityNamesReader; import org.hibernate.envers.revisioninfo.RevisionInfoGenerator; import org.hibernate.envers.revisioninfo.RevisionInfoNumberReader; import org.hibernate.envers.revisioninfo.RevisionInfoQueryCreator; import org.hibernate.envers.tools.MutableBoolean; import org.hibernate.mapping.PersistentClass; import org.hibernate.type.LongType; import org.hibernate.type.Type; /** * @author Adam Warski (adam at warski dot org) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class RevisionInfoConfiguration { private String revisionInfoEntityName; private PropertyData revisionInfoIdData; private PropertyData revisionInfoTimestampData; private PropertyData modifiedEntityNamesData; private Type revisionInfoTimestampType; private GlobalConfiguration globalCfg; private String revisionPropType; private String revisionPropSqlType; public RevisionInfoConfiguration(GlobalConfiguration globalCfg) { this.globalCfg = globalCfg; revisionInfoEntityName = "org.hibernate.envers.DefaultRevisionEntity"; revisionInfoIdData = new PropertyData("id", "id", "field", null); revisionInfoTimestampData = new PropertyData("timestamp", "timestamp", "field", null); modifiedEntityNamesData = new PropertyData("modifiedEntityNames", "modifiedEntityNames", "field", null); revisionInfoTimestampType = new LongType(); revisionPropType = "integer"; } private Document generateDefaultRevisionInfoXmlMapping() { Document document = DocumentHelper.createDocument(); Element class_mapping = MetadataTools.createEntity(document, new AuditTableData(null, null, globalCfg.getDefaultSchemaName(), globalCfg.getDefaultCatalogName()), null); class_mapping.addAttribute("name", revisionInfoEntityName); class_mapping.addAttribute("table", "REVINFO"); Element idProperty = MetadataTools.addNativelyGeneratedId(class_mapping, revisionInfoIdData.getName(), revisionPropType); MetadataTools.addColumn(idProperty, "REV", null, 0, 0, null, null, null, false); Element timestampProperty = MetadataTools.addProperty(class_mapping, revisionInfoTimestampData.getName(), revisionInfoTimestampType.getName(), true, false); MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null, false); if (globalCfg.isTrackEntitiesChangedInRevisionEnabled()) { generateEntityNamesTrackingTableMapping(class_mapping, "modifiedEntityNames", globalCfg.getDefaultSchemaName(), globalCfg.getDefaultCatalogName(), "REVCHANGES", "REV", "ENTITYNAME", "string"); } return document; } /** * Generates mapping that represents a set of primitive types.<br /> * <code> * <set name="propertyName" table="joinTableName" schema="joinTableSchema" catalog="joinTableCatalog" * cascade="persist, delete" lazy="false" fetch="join"><br /> * <key column="joinTablePrimaryKeyColumnName" /><br /> * <element type="joinTableValueColumnType"><br /> * <column name="joinTableValueColumnName" /><br /> * </element><br /> * </set> * </code> */ private void generateEntityNamesTrackingTableMapping(Element class_mapping, String propertyName, String joinTableSchema, String joinTableCatalog, String joinTableName, String joinTablePrimaryKeyColumnName, String joinTableValueColumnName, String joinTableValueColumnType) { Element set = class_mapping.addElement("set"); set.addAttribute("name", propertyName); set.addAttribute("table", joinTableName); set.addAttribute("schema", joinTableSchema); set.addAttribute("catalog", joinTableCatalog); set.addAttribute("cascade", "persist, delete"); set.addAttribute("fetch", "join"); set.addAttribute("lazy", "false"); Element key = set.addElement("key"); key.addAttribute("column", joinTablePrimaryKeyColumnName); Element element = set.addElement("element"); element.addAttribute("type", joinTableValueColumnType); Element column = element.addElement("column"); column.addAttribute("name", joinTableValueColumnName); } private Element generateRevisionInfoRelationMapping() { Document document = DocumentHelper.createDocument(); Element rev_rel_mapping = document.addElement("key-many-to-one"); rev_rel_mapping.addAttribute("type", revisionPropType); rev_rel_mapping.addAttribute("class", revisionInfoEntityName); if (revisionPropSqlType != null) { // Putting a fake name to make Hibernate happy. It will be replaced later anyway. MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType, null, null, false); } return rev_rel_mapping; } private void searchForRevisionInfoCfgInProperties(XClass clazz, ReflectionManager reflectionManager, MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound, MutableBoolean modifiedEntityNamesFound, String accessType) { for (XProperty property : clazz.getDeclaredProperties(accessType)) { RevisionNumber revisionNumber = property.getAnnotation(RevisionNumber.class); RevisionTimestamp revisionTimestamp = property.getAnnotation(RevisionTimestamp.class); ModifiedEntityNames modifiedEntityNames = property.getAnnotation(ModifiedEntityNames.class); if (revisionNumber != null) { if (revisionNumberFound.isSet()) { throw new MappingException("Only one property may be annotated with @RevisionNumber!"); } XClass revisionNumberClass = property.getType(); if (reflectionManager.equals(revisionNumberClass, Integer.class) || reflectionManager.equals(revisionNumberClass, Integer.TYPE)) { revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null); revisionNumberFound.set(); } else if (reflectionManager.equals(revisionNumberClass, Long.class) || reflectionManager.equals(revisionNumberClass, Long.TYPE)) { revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null); revisionNumberFound.set(); // The default is integer revisionPropType = "long"; } else { throw new MappingException("The field annotated with @RevisionNumber must be of type " + "int, Integer, long or Long"); } // Getting the @Column definition of the revision number property, to later use that info to // generate the same mapping for the relation from an audit table's revision number to the // revision entity revision number. Column revisionPropColumn = property.getAnnotation(Column.class); if (revisionPropColumn != null) { revisionPropSqlType = revisionPropColumn.columnDefinition(); } } if (revisionTimestamp != null) { if (revisionTimestampFound.isSet()) { throw new MappingException("Only one property may be annotated with @RevisionTimestamp!"); } XClass revisionTimestampClass = property.getType(); if (reflectionManager.equals(revisionTimestampClass, Long.class) || reflectionManager.equals(revisionTimestampClass, Long.TYPE) || reflectionManager.equals(revisionTimestampClass, Date.class) || reflectionManager.equals(revisionTimestampClass, java.sql.Date.class)) { revisionInfoTimestampData = new PropertyData(property.getName(), property.getName(), accessType, null); revisionTimestampFound.set(); } else { throw new MappingException("The field annotated with @RevisionTimestamp must be of type " + "long, Long, java.util.Date or java.sql.Date"); } } if (modifiedEntityNames != null) { if (modifiedEntityNamesFound.isSet()) { throw new MappingException("Only one property may be annotated with @ModifiedEntityNames!"); } XClass modifiedEntityNamesClass = property.getType(); if (reflectionManager.equals(modifiedEntityNamesClass, Set.class) && reflectionManager.equals(property.getElementClass(), String.class)) { modifiedEntityNamesData = new PropertyData(property.getName(), property.getName(), accessType, null); modifiedEntityNamesFound.set(); } else { throw new MappingException("The field annotated with @ModifiedEntityNames must be of Set<String> type."); } } } } private void searchForRevisionInfoCfg(XClass clazz, ReflectionManager reflectionManager, MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound, MutableBoolean modifiedEntityNamesFound) { XClass superclazz = clazz.getSuperclass(); if (!"java.lang.Object".equals(superclazz.getName())) { searchForRevisionInfoCfg(superclazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityNamesFound); } searchForRevisionInfoCfgInProperties(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityNamesFound, "field"); searchForRevisionInfoCfgInProperties(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityNamesFound, "property"); } public RevisionInfoConfigurationResult configure(Configuration cfg, ReflectionManager reflectionManager) { Iterator<PersistentClass> classes = (Iterator Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate RevisionInfoConfiguration.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.