All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.envers.boot.model.Join Maven / Gradle / Ivy

There is a newer version: 7.0.0.Beta2
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.envers.boot.model;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmKeyType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSecondaryTableType;
import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.Value;

/**
 * Contract that represents a secondary table join that is part of an entity hierarchy.
 *
 * @author Chris Cranford
 */
public class Join implements AttributeContainer, Bindable {

	private final List keyColumns;
	private final List attributes;

	private boolean inverse;
	private boolean optional;
	private String tableName;
	private String schema;
	private String catalog;

	public Join(String catalogName, String schemaName, String tableName) {
		this.catalog = catalogName;
		this.schema = schemaName;
		this.tableName = tableName;
		this.keyColumns = new ArrayList<>();
		this.attributes = new ArrayList<>();
	}

	@Override
	public void addAttribute(Attribute attribute) {
		this.attributes.add( attribute );
	}

	public void setTable(String tableName) {
		this.tableName = tableName;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public void setOptional(boolean optional) {
		this.optional = optional;
	}

	public void setInverse(boolean inverse) {
		this.inverse = inverse;
	}

	public void addKeyColumn(Column keyColumn) {
		this.keyColumns.add( keyColumn );
	}

	public void addKeyColumnsFromValue(Value value) {
		final List selectables = value.getSelectables();
		for ( Selectable s : selectables ) {
			keyColumns.add( Column.from( s ) );
		}
	}

	@Override
	public JaxbHbmSecondaryTableType build() {
		final JaxbHbmSecondaryTableType join = new JaxbHbmSecondaryTableType();

		if ( !StringTools.isEmpty( catalog ) ) {
			join.setCatalog( catalog );
		}

		if ( !StringTools.isEmpty( schema ) ) {
			join.setSchema( schema );
		}

		join.setTable( tableName );
		join.setOptional( optional );
		join.setInverse( inverse );

		final JaxbHbmKeyType key = new JaxbHbmKeyType();
		join.setKey( key );

		for ( Column keyColumn : keyColumns ) {
			key.getColumn().add( keyColumn.build() );
		}

		for ( Attribute attribute : attributes ) {
			join.getAttributes().add( attribute.build() );
		}

		return join;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy