Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5d6c236756c4aa521fc90b701953be9d9345ce64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.gen.internal;

import org.eclipse.jpt.db.ForeignKey;

/**
 * This object is shared by the two gen tables that make up the relation.
 * Upon construction, 'mappedBy' will be 'null'. The first gen table to be
 * used to generate an entity will fill in 'mappedBy' with the appropriate
 * field/property name.
 */
class ManyToManyRelation {
	private final GenTable joinTable;
	private final ForeignKey owningForeignKey;
	private final GenTable owningTable;
	private final ForeignKey nonOwningForeignKey;
	private final GenTable nonOwningTable;
	private String mappedBy;


	ManyToManyRelation(GenTable joinTable, ForeignKey owningForeignKey, GenTable owningTable, ForeignKey nonOwningForeignKey, GenTable nonOwningTable) {
		super();
		this.joinTable = joinTable;

		this.owningForeignKey = owningForeignKey;
		this.owningTable = owningTable;
		owningTable.addOwnedManyToManyRelation(this);

		this.nonOwningForeignKey = nonOwningForeignKey;
		this.nonOwningTable = nonOwningTable;
		nonOwningTable.addNonOwnedManyToManyRelation(this);
	}

	GenTable getJoinTable() {
		return this.joinTable;
	}

	ForeignKey getOwningForeignKey() {
		return this.owningForeignKey;
	}

	GenTable getOwningTable() {
		return this.owningTable;
	}

	ForeignKey getNonOwningForeignKey() {
		return this.nonOwningForeignKey;
	}

	GenTable getNonOwningTable() {
		return this.nonOwningTable;
	}

	private GenTable otherTable(GenTable table) {
		return (table == this.owningTable) ? this.nonOwningTable : this.owningTable;
	}

	String javaFieldNameFor(GenTable table) {
		// TODO i18n?
		return this.otherTable(table).javaFieldName() + "_collection";
	}

	void clear() {
		this.owningTable.removeOwnedManyToManyRelation(this);
		this.nonOwningTable.removeNonOwnedManyToManyRelation(this);
	}

	String getMappedBy() {
		return this.mappedBy;
	}

	void setMappedBy(String mappedBy) {
		this.mappedBy = mappedBy;
	}

	String owningEntityName() {
		return this.owningTable.getEntityName();
	}

	String nonOwningEntityName() {
		return this.nonOwningTable.getEntityName();
	}

	boolean joinTableNameIsDefault() {
		return this.joinTable.name().equals(this.getOwningTable().name() + "_" + this.getNonOwningTable().name());
	}

	boolean joinColumnsIsDefaultFor(String javaFieldName) {
		return this.owningForeignKey.isDefaultFor(javaFieldName);
	}

	boolean inverseJoinColumnsIsDefaultFor(String javaFieldName) {
		return this.nonOwningForeignKey.isDefaultFor(javaFieldName);
	}

}

Back to the top