View | Details | Raw Unified | Return to bug 395738 | Differences between
and this patch

Collapse All | Expand All

(-)a/jpa/eclipselink.jpa.test/antbuild.xml (+1 lines)
Lines 843-848 Link Here
843
                               org/eclipse/persistence/testing/models/jpa/cacheable/**
843
                               org/eclipse/persistence/testing/models/jpa/cacheable/**
844
                               org/eclipse/persistence/testing/models/jpa/cascadedeletes/**
844
                               org/eclipse/persistence/testing/models/jpa/cascadedeletes/**
845
                               org/eclipse/persistence/testing/models/jpa/complexaggregate/**
845
                               org/eclipse/persistence/testing/models/jpa/complexaggregate/**
846
                               org/eclipse/persistence/testing/models/jpa/customer/**
846
                               org/eclipse/persistence/testing/models/jpa/datatypes/**
847
                               org/eclipse/persistence/testing/models/jpa/datatypes/**
847
                               org/eclipse/persistence/testing/models/jpa/datetime/**
848
                               org/eclipse/persistence/testing/models/jpa/datetime/**
848
                               org/eclipse/persistence/testing/models/jpa/inheritance/**
849
                               org/eclipse/persistence/testing/models/jpa/inheritance/**
(-)a/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/osgi/persistence.xml (+20 lines)
Lines 142-145 Link Here
142
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
142
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
143
        <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
143
        <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
144
    </persistence-unit>
144
    </persistence-unit>
145
146
    <!-- Advanced customer model persistence unit. -->
147
    <persistence-unit name="customer-pu" transaction-type="RESOURCE_LOCAL">
148
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
149
        <shared-cache-mode>NONE</shared-cache-mode>
150
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.CustomerAddress</class>
151
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType1</class>
152
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType2</class>
153
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.RegisteredCustomer</class>
154
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
155
        <properties>
156
            <property name="eclipselink.logging.level" value="FINE" />
157
            <property name="eclipselink.logging.level.sql" value="FINEST"/>
158
            <property name="eclipselink.logging.level.cache" value="FINEST" />
159
            <property name="eclipselink.logging.parameters" value="true"/>
160
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
161
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
162
        </properties>
163
    </persistence-unit>
164
145
</persistence>
165
</persistence>
(-)a/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/persistence.xml (+20 lines)
Lines 403-406 Link Here
403
			<property name="eclipselink.validation-only" value="true"/>
403
			<property name="eclipselink.validation-only" value="true"/>
404
		</properties>
404
		</properties>
405
	</persistence-unit>
405
	</persistence-unit>
406
407
    <!-- Advanced customer model persistence unit. -->
408
    <persistence-unit name="customer-pu" transaction-type="RESOURCE_LOCAL">
409
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
410
        <shared-cache-mode>NONE</shared-cache-mode>
411
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.CustomerAddress</class>
412
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType1</class>
413
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType2</class>
414
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.RegisteredCustomer</class>
415
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
416
        <properties>
417
            <property name="eclipselink.logging.level" value="FINE" />
418
            <property name="eclipselink.logging.level.sql" value="FINEST"/>
419
            <property name="eclipselink.logging.level.cache" value="FINEST" />
420
            <property name="eclipselink.logging.parameters" value="true"/>
421
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
422
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
423
        </properties>
424
    </persistence-unit>
425
406
</persistence>
426
</persistence>
(-)a/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/spring/persistence.xml (+20 lines)
Lines 216-219 Link Here
216
            <property name="eclipselink.session-name" value="additional-criteria"/>
216
            <property name="eclipselink.session-name" value="additional-criteria"/>
217
        </properties>
217
        </properties>
218
    </persistence-unit>
218
    </persistence-unit>
219
220
    <!-- Advanced customer model persistence unit. -->
221
    <persistence-unit name="customer-pu" transaction-type="RESOURCE_LOCAL">
222
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
223
        <shared-cache-mode>NONE</shared-cache-mode>
224
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.CustomerAddress</class>
225
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType1</class>
226
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType2</class>
227
        <class>org.eclipse.persistence.testing.models.jpa.advanced.customer.RegisteredCustomer</class>
228
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
229
        <properties>
230
            <property name="eclipselink.logging.level" value="FINE" />
231
            <property name="eclipselink.logging.level.sql" value="FINEST"/>
232
            <property name="eclipselink.logging.level.cache" value="FINEST" />
233
            <property name="eclipselink.logging.parameters" value="true"/>
234
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
235
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
236
        </properties>
237
    </persistence-unit>
238
219
</persistence>
239
</persistence>
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/advanced/customer/AddressType1.java (+72 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     jlamande    - Initial API and implementation
12
 *     Tomas Kraus - EclipseLink jUnit tests integration
13
 ******************************************************************************/
14
package org.eclipse.persistence.testing.models.jpa.advanced.customer;
15
16
import javax.persistence.Column;
17
import javax.persistence.DiscriminatorValue;
18
import javax.persistence.Entity;
19
20
/**
21
 * First abstract {@link CustomerAddress} entity offspring.
22
 */
23
@Entity
24
@DiscriminatorValue("1")
25
public class AddressType1 extends CustomerAddress {
26
27
    /** Street name. */
28
    @Column(name="STREET1")
29
    private String street1;
30
31
    /**
32
     * Creates an instance of first {@link CustomerAddress} entity offspring. No entity attributes are set.
33
     */
34
    public AddressType1() {
35
    }
36
37
    /**
38
     * Creates an instance of first {@link CustomerAddress} entity offspring.
39
     * @param street Street name to be set.
40
     */
41
    public AddressType1(final String street) {
42
        street1 = street;
43
    }
44
45
    /**
46
     * Get street name.
47
     * @return Street name attribute value.
48
     */
49
    @Override
50
    public String getStreet() {
51
        return street1;
52
    }
53
54
    /**
55
     * Set street name.
56
     * @param street Street name to be set.
57
     */
58
    @Override
59
    public void setStreet(final String street) {
60
        street1 = street;
61
    }
62
63
    /**
64
     * Return first {@link CustomerAddress} entity offspring in human readable form.
65
     * @return Entity converted to {@link String} in human readable form.
66
     */
67
    @Override
68
    public String toString() {
69
        return "AddressType1 [" + street1 + "]";
70
    }
71
72
}
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/advanced/customer/AddressType2.java (+71 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     jlamande    - Initial API and implementation
12
 *     Tomas Kraus - EclipseLink jUnit tests integration
13
 ******************************************************************************/
14
package org.eclipse.persistence.testing.models.jpa.advanced.customer;
15
16
import javax.persistence.Column;
17
import javax.persistence.DiscriminatorValue;
18
import javax.persistence.Entity;
19
20
/**
21
 * Second abstract {@link CustomerAddress} entity offspring.
22
 */
23
@Entity
24
@DiscriminatorValue("2")
25
public class AddressType2 extends CustomerAddress {
26
27
    /** Street name. */
28
    @Column(name="STREET2")
29
    private String street2;
30
31
    /**
32
     * Creates an instance of second {@link CustomerAddress} entity offspring. No entity attributes are set.
33
     */
34
    public AddressType2() {
35
    }
36
37
    /**
38
     * Creates an instance of second {@link CustomerAddress} entity offspring.
39
     * @param street Street name to be set.
40
     */
41
    public AddressType2(final String street) {
42
        this.street2 = street;
43
    }
44
45
    /**
46
     * Get street name.
47
     * @return Street name attribute value.
48
     */
49
    @Override
50
    public String getStreet() {
51
        return street2;
52
    }
53
54
    /**
55
     * Set street name.
56
     * @param street Street name to be set.
57
     */
58
    @Override
59
    public void setStreet(final String street) {
60
        street2 = street;
61
    }
62
63
    /**
64
     * Return secomd {@link CustomerAddress} entity offspring in human readable form.
65
     * @return Entity converted to {@link String} in human readable form.
66
     */
67
    @Override
68
    public String toString() {
69
        return "AddressType2 [" + street2 + "]";
70
    }
71
}
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/advanced/customer/AdvancedCustomerTableCreator.java (+62 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     Tomas Kraus - Initial API and implementation
12
 ******************************************************************************/
13
package org.eclipse.persistence.testing.models.jpa.advanced.customer;
14
15
import org.eclipse.persistence.testing.framework.TogglingFastTableCreator;
16
import org.eclipse.persistence.tools.schemaframework.TableDefinition;
17
18
// This table creator class is not used in the tests. Persistence unit shall create tables itself because of having
19
// eclipselink.ddl-generation value set to drop-and-create-tables.
20
// TODO: Resolve sequence / auto increment for primary keys. This is not implemented yet in this table creator.
21
/**
22
 * Advanced customer model table creator.
23
 */
24
public class AdvancedCustomerTableCreator extends TogglingFastTableCreator {
25
26
    /**
27
     * Build Address entity table definition including DiscriminatorColumn and offspring columns.
28
     * @return Address entity table definition.
29
     */
30
    public static TableDefinition buildAddressTable() {
31
        TableDefinition table = createTable("CMP3_AD_CU_ADDRESS");
32
        table.addField(createNumericPk("ID"));
33
        table.addField(createNumericColumn("TYPE", 1, false));
34
        table.addField(createStringColumn("STREET1"));
35
        table.addField(createStringColumn("STREET2"));
36
        return table;
37
    }
38
39
    /**
40
     * Build Customer entity table definition.
41
     * @return Customer entity table definition.
42
     */
43
    public static TableDefinition buildCustomerTable() {
44
        TableDefinition table = createTable("CMP3_AD_CU_ADDRESS");
45
        table.addField(createNumericPk("ID"));
46
        table.addField(createStringColumn("NAME", 1, false));
47
        table.addField(createNumericFk("ADDR_BA_ID", "ID"));
48
        table.addField(createNumericFk("ADDR_DA_ID", "ID"));
49
        return table;
50
    }
51
52
    /**
53
     * Crates an instance of advanced customer model table creator.
54
     * All model table definitions are registered.
55
     */
56
    public AdvancedCustomerTableCreator() {
57
        setName("EJB3AdvancedCustomerProject");
58
        addTableDefinition(buildAddressTable());
59
        addTableDefinition(buildCustomerTable());
60
    }
61
62
}
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/advanced/customer/CustomerAddress.java (+54 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     jlamande    - Initial API and implementation
12
 *     Tomas Kraus - EclipseLink jUnit tests integration
13
 ******************************************************************************/
14
package org.eclipse.persistence.testing.models.jpa.advanced.customer;
15
16
import javax.persistence.Column;
17
import javax.persistence.DiscriminatorColumn;
18
import javax.persistence.Entity;
19
import javax.persistence.GeneratedValue;
20
import javax.persistence.GenerationType;
21
import javax.persistence.Id;
22
import javax.persistence.Inheritance;
23
import javax.persistence.SequenceGenerator;
24
import javax.persistence.Table;
25
26
/**
27
 * Abstract customer's address entity. 
28
 */
29
@Entity
30
@Table(name="CMP3_AD_CU_ADDRESS")
31
@Inheritance
32
@DiscriminatorColumn(name="TYPE",length=1)
33
@SequenceGenerator(name="CMP3_AD_CU_ADDRESS_ID_SEQ")
34
public abstract class CustomerAddress {
35
36
    /** Address primary key. */
37
	@Id
38
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CMP3_AD_CU_ADDRESS_ID_SEQ")
39
	@Column(name="ID")
40
	private long id;
41
42
    /**
43
     * Get street name.
44
     * @return Street name attribute value.
45
     */
46
	public abstract String getStreet();
47
48
    /**
49
     * Set street name.
50
     * @param street Street name to be set.
51
     */
52
    public abstract void setStreet(final String street);
53
54
}
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/advanced/customer/RegisteredCustomer.java (+156 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     jlamande    - Initial API and implementation
12
 *     Tomas Kraus - EclipseLink jUnit tests integration
13
 ******************************************************************************/
14
package org.eclipse.persistence.testing.models.jpa.advanced.customer;
15
16
import javax.persistence.CascadeType;
17
import javax.persistence.Column;
18
import javax.persistence.Entity;
19
import javax.persistence.GeneratedValue;
20
import javax.persistence.GenerationType;
21
import javax.persistence.Id;
22
import javax.persistence.JoinColumn;
23
import javax.persistence.NamedQueries;
24
import javax.persistence.NamedQuery;
25
import javax.persistence.OneToOne;
26
import javax.persistence.SequenceGenerator;
27
import javax.persistence.Table;
28
29
/**
30
 * Customer entity. 
31
 */
32
@Entity
33
@Table(name="CMP3_AD_CU_CUSTOMER")
34
@SequenceGenerator(name="CMP3_AD_CU_CUSTOMER_ID_SEQ")
35
@NamedQueries({
36
    @NamedQuery(name="RegisteredCustomer.selecAll",
37
            query="SELECT c FROM RegisteredCustomer c"),
38
39
    @NamedQuery(name="RegisteredCustomer.selecByName",
40
            query="SELECT c FROM RegisteredCustomer c"
41
                + " WHERE c.name = :name"),
42
43
    @NamedQuery(name="RegisteredCustomer.selecByNameFetch",
44
            query="SELECT c FROM RegisteredCustomer c"
45
                + "  LEFT JOIN FETCH c.deliveryAddress"
46
                + "  LEFT JOIN FETCH c.billingAddress"
47
                + " WHERE c.name = :name")
48
})
49
public class RegisteredCustomer {
50
51
    /** Customer primary key. */
52
    @Id
53
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CMP3_AD_CU_CUSTOMER_ID_SEQ")
54
    @Column(name="ID")
55
    private long id;
56
57
    /** Customer name. */
58
    @Column(name="NAME")
59
    private String name;
60
61
    /** Customer billing address. */
62
    @OneToOne(cascade=CascadeType.ALL)
63
    @JoinColumn(name="ADDR_BA_ID", referencedColumnName="ID")
64
    private CustomerAddress billingAddress;
65
66
    /** Customer delivery address. */
67
    @OneToOne(cascade=CascadeType.ALL)
68
    @JoinColumn(name="ADDR_DA_ID", referencedColumnName="ID")
69
    private CustomerAddress deliveryAddress;
70
71
    /**
72
     * Creates an instance of {@code RegisteredCustomer} entity.
73
     * No entity attributes are set.
74
     */
75
    public RegisteredCustomer() {
76
    }
77
78
    /**
79
     * Creates an instance of {@code RegisteredCustomer} entity.
80
     * @param name            Customer name to be set.
81
     * @param billingAddress  Customer billing address to be set.
82
     * @param deliveryAddress Customer delivery address to be set.
83
     */
84
    public RegisteredCustomer(final String name, final CustomerAddress billingAddress,
85
            final CustomerAddress deliveryAddress) {
86
        this.name = name;
87
        this.billingAddress = billingAddress;
88
        this.deliveryAddress = deliveryAddress;
89
    }
90
91
    /**
92
     * Get Customer primary key.
93
     * @return Customer primary key attribute value.
94
     */
95
    public long getId() {
96
        return id;
97
    }
98
99
    /**
100
     * Get Customer name.
101
     * @return Customer name attribute value.
102
     */
103
    public String getName() {
104
        return name;
105
    }
106
107
    /**
108
     * Set Customer name.
109
     * @return Customer name to be set.
110
     */
111
    public void setName(final String name) {
112
        this.name = name;
113
    }
114
115
    /**
116
     * Get Customer billing address.
117
     * @return Customer billing address attribute value.
118
     */
119
    public CustomerAddress getBillingAddress() {
120
        return billingAddress;
121
    }
122
123
    /**
124
     * Set Customer billing address.
125
     * @param billingAddress Customer billing address to be set.
126
     */
127
    public void setBillingAddress(final CustomerAddress billingAddress) {
128
        this.billingAddress = billingAddress;
129
    }
130
131
    /**
132
     * Get Customer delivery address.
133
     * @return Customer delivery address attribute value.
134
     */
135
    public CustomerAddress getDeliveryAddress() {
136
        return deliveryAddress;
137
    }
138
139
    /**
140
     * Set Customer delivery address.
141
     * @param deliveryAddress Customer delivery address to be set.
142
     */
143
    public void setDeliveryAddress(final CustomerAddress deliveryAddress) {
144
        this.deliveryAddress = deliveryAddress;
145
    }
146
147
    /**
148
     * Return Customer entity in human readable form.
149
     * @return Entity converted to {@link String} in human readable form.
150
     */
151
    @Override
152
    public String toString() {
153
        return "RegisteredCustomer [" + id + "," + name + "," + billingAddress + "," + deliveryAddress + "]";
154
    }
155
156
}
(-)a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/advanced/AbstractEntityWithColumnDiscriminatorTest.java (-1 / +260 lines)
Added Link Here
0
- 
1
/*******************************************************************************
2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     jlamande    - Initial API and implementation
12
 *     Tomas Kraus - EclipseLink jUnit tests integration
13
 ******************************************************************************/
14
package org.eclipse.persistence.testing.tests.jpa.advanced;
15
16
import java.util.List;
17
18
import javax.persistence.EntityManager;
19
import javax.persistence.Query;
20
import javax.persistence.TypedQuery;
21
import javax.persistence.criteria.CriteriaBuilder;
22
import javax.persistence.criteria.CriteriaQuery;
23
import javax.persistence.criteria.JoinType;
24
import javax.persistence.criteria.Root;
25
26
import junit.framework.Test;
27
import junit.framework.TestSuite;
28
29
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase;
30
import org.eclipse.persistence.testing.models.jpa.advanced.AdvancedTableCreator;
31
import org.eclipse.persistence.testing.models.jpa.advanced.customer.CustomerAddress;
32
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType1;
33
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType2;
34
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AdvancedCustomerTableCreator;
35
import org.eclipse.persistence.testing.models.jpa.advanced.customer.RegisteredCustomer;
36
37
/**
38
 * Runs tests on advanced customer model with Customer entity having two abstract Addresses. Each of those addresses
39
 * may contain instances of different offspring. Address entities are mapped into a single table using
40
 * {@code DiscriminatorColumn} to distinguish them.
41
 */
42
public class AbstractEntityWithColumnDiscriminatorTest extends JUnitTestCase {
43
44
    /** {@link RegisteredCustomer} entity name attribute value. */
45
    private static final String CUSTOMER_NAME = "JONES";
46
47
    /** Value of {@code billingAddress} entity street attribute. */
48
    private static final String CUSTOMER_ADDR_BILLING = "STREET1";
49
50
    /** Value of {@code deliveryAddress} entity street attribute. */
51
    private static final String CUSTOMER_ADDR_DELIVERY = "STREET2";
52
53
54
    /** Persistence unit name. */
55
    private static final String PU_NAME = "customer-pu";
56
57
    /**
58
     * Build collection of test cases for this jUnit test instance.
59
     * @return {@link Test} class containing collection of test cases for this jUnit test instance.
60
     */
61
    public static Test suite() {
62
        final TestSuite suite = new TestSuite();
63
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testSetup"));
64
        addNamedQueryTests(suite);
65
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCleanup"));
66
        return suite;
67
    }
68
69
    /**
70
     * Adds test, similar to suite() but without adding a setup and cleanup. Used from {@code suite()} to add individual
71
     * tests.
72
     * @param suite Target {@link TestSuite} class where to store tests.
73
     * @return {@link Test} class containing collection of test cases for this jUnit test instance.
74
     */
75
    public static Test addNamedQueryTests(final TestSuite suite){
76
        suite.setName("NamedQueryJUnitTest");
77
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testJpqlNoFetch"));
78
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testJpqlFetch"));
79
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCriteriaNoFetch"));
80
        suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCriteriaFetch"));
81
        return suite;
82
    }
83
84
    /**
85
     * Create {@link RegisteredCustomer} entity instance with {@code billingAddress} set to {@link AddressType1}
86
     * instance and {@code deliveryAddress} set to {@link AddressType2} instance. Entity is persisted.
87
     * @param em An {@link EntityManager} instance used to persist entities.
88
     */
89
    private static void createCustomer(final EntityManager em) {
90
        em.getTransaction().begin();
91
        try {
92
            final RegisteredCustomer cus = new RegisteredCustomer(CUSTOMER_NAME, new AddressType1(
93
                    CUSTOMER_ADDR_BILLING), new AddressType2(CUSTOMER_ADDR_DELIVERY));
94
            em.persist(cus);
95
            em.getTransaction().commit();
96
        } catch (Throwable e) {
97
            em.getTransaction().rollback();
98
            throw e;
99
        }
100
    }
101
102
    /**
103
     * Delete all {@link RegisteredCustomer} entities.
104
     * @param em An {@link EntityManager} instance used to persist entities.
105
     */
106
    private static void deleteAllCustomers(final EntityManager em) {
107
        List<RegisteredCustomer> customers = em.createNamedQuery(
108
                "RegisteredCustomer.selecAll", RegisteredCustomer.class).getResultList();
109
        em.getTransaction().begin();
110
        try {
111
            for (RegisteredCustomer cus : customers) {
112
                em.remove(cus);
113
            }
114
            em.getTransaction().commit();
115
        } catch (Throwable e) {
116
            em.getTransaction().rollback();
117
            throw e;
118
        }
119
    }
120
121
    /**
122
     * Validate {@link RegisteredCustomer} entity instance.
123
     * @param customer {@link RegisteredCustomer} entity instance to be validated.
124
     */
125
    private static void assertCustomer(final RegisteredCustomer customer) {
126
        assertEquals(CUSTOMER_NAME, customer.getName());
127
        assertTrue(customer.getBillingAddress() instanceof AddressType1);
128
        assertEquals(CUSTOMER_ADDR_BILLING, ((AddressType1) customer.getBillingAddress()).getStreet());
129
        assertTrue(customer.getDeliveryAddress() instanceof AddressType2);
130
        assertEquals(CUSTOMER_ADDR_DELIVERY, ((AddressType2) customer.getDeliveryAddress()).getStreet());
131
    }
132
133
    /**
134
     * Constructs an instance of {@code AbstractEntityWithColumnDiscriminatorTest} class.
135
     */
136
    public AbstractEntityWithColumnDiscriminatorTest() {
137
        super();
138
    }
139
140
    /**
141
     * Constructs an instance of {@code AbstractEntityWithColumnDiscriminatorTest} class with given test case name.
142
     * @param name Test case name.
143
     */
144
    public AbstractEntityWithColumnDiscriminatorTest(final String name) {
145
        super(name);
146
        setPuName(PU_NAME);
147
    }
148
149
    /**
150
     * Initial setup is done as first test in collection, both to record its failure, and to allow execution
151
     * in the server.
152
     */
153
    public void testSetup() {
154
        // Table creator shall not be used for this persistence unit.
155
        // new AdvancedCustomerTableCreator().replaceTables(getServerSession(PU_NAME));
156
        clearCache(PU_NAME);
157
        final EntityManager em = createEntityManager(PU_NAME);
158
        try {
159
            createCustomer(em);
160
        } finally {
161
            em.close();
162
        }
163
    }
164
165
    /**
166
     * Final cleanup is done as last test in collection, both to record its failure, and to allow execution
167
     * in the server.
168
     */
169
    public void testCleanup() {
170
        final EntityManager em = createEntityManager(PU_NAME);
171
        try {
172
            deleteAllCustomers(em);
173
        } finally {
174
            em.close();
175
        }
176
    }
177
178
    /**
179
     * Test JPQL query on {@link RegisteredCustomer} without {@code LEFT JOIN FETCH} of {@link CustomerAddress}
180
     * entities.
181
     */
182
    public void testJpqlNoFetch() {
183
        final EntityManager em = createEntityManager(PU_NAME);
184
        try {
185
            final Query query = em.createNamedQuery(
186
                    "RegisteredCustomer.selecByName",
187
                    RegisteredCustomer.class).setParameter("name", CUSTOMER_NAME);
188
            final RegisteredCustomer customer = (RegisteredCustomer) query.getSingleResult();
189
            assertCustomer(customer);
190
        } finally {
191
            em.close();
192
        }
193
    }
194
195
    /**
196
     * Test JPQL query on {@link RegisteredCustomer} with {@code LEFT JOIN FETCH} of {@link CustomerAddress} entities.
197
     */
198
    public void testJpqlFetch() {
199
        final EntityManager em = createEntityManager(PU_NAME);
200
        try {
201
            final Query query = em.createNamedQuery(
202
                    "RegisteredCustomer.selecByNameFetch",
203
                    RegisteredCustomer.class).setParameter("name", CUSTOMER_NAME);
204
            final RegisteredCustomer customer = (RegisteredCustomer) query.getSingleResult();
205
            assertCustomer(customer);
206
        } finally {
207
            em.close();
208
        }
209
    }
210
211
    /**
212
     * Test criteria query on {@link RegisteredCustomer} without {@code LEFT JOIN FETCH} of {@link CustomerAddress}
213
     * entities.
214
     */
215
    public void testCriteriaNoFetch() {
216
        final EntityManager em = createEntityManager(PU_NAME);
217
        try {
218
            final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
219
            final CriteriaQuery<RegisteredCustomer> criteriaQuery
220
                    = criteriaBuilder.createQuery(RegisteredCustomer.class);
221
            final Root<RegisteredCustomer> from = criteriaQuery.from(RegisteredCustomer.class);
222
223
            criteriaQuery.where(criteriaBuilder.equal(from.get("name"), CUSTOMER_NAME));
224
            criteriaQuery.distinct(true);
225
226
            final TypedQuery<RegisteredCustomer> query = em.createQuery(criteriaQuery);
227
            final RegisteredCustomer customer = query.getSingleResult();
228
            assertCustomer(customer);
229
        } finally {
230
            em.close();
231
        }
232
    }
233
234
    /**
235
     * Test criteria query on {@link RegisteredCustomer} with {@code LEFT JOIN FETCH} of {@link CustomerAddress}
236
     * entities.
237
     */
238
    public void testCriteriaFetch() {
239
        final EntityManager em = createEntityManager(PU_NAME);
240
        try {
241
            final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
242
            final CriteriaQuery<RegisteredCustomer> criteriaQuery
243
                    = criteriaBuilder.createQuery(RegisteredCustomer.class);
244
            final Root<RegisteredCustomer> from = criteriaQuery.from(RegisteredCustomer.class);
245
246
            from.fetch("billingAddress", JoinType.LEFT);
247
            from.fetch("deliveryAddress", JoinType.LEFT);
248
249
            criteriaQuery.where(criteriaBuilder.equal(from.get("name"), CUSTOMER_NAME));
250
            criteriaQuery.distinct(true);
251
252
            final TypedQuery<RegisteredCustomer> query = em.createQuery(criteriaQuery);
253
            final RegisteredCustomer customer = query.getSingleResult();
254
            assertCustomer(customer);
255
        } finally {
256
            em.close();
257
        }
258
    }
259
260
}

Return to bug 395738