Skip to main content
summaryrefslogtreecommitdiffstats
blob: b90d8c4b579421033edac9714e7502089ff4f977 (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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.examples.hibernate.client;

import org.eclipse.emf.cdo.examples.company.CompanyPackage;
import org.eclipse.emf.cdo.server.hibernate.teneo.CDOMappingGenerator;

import org.eclipse.net4j.util.io.IOUtil;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.PersistenceOptions;

import java.util.Properties;

import junit.framework.TestCase;

/**
 * This class shows how the hibernate mapping can be generated programmatically. The hibernate mapping can be stored
 * locally and used in the server.
 * <p/>
 * See <a href=
 * "http://wiki.eclipse.org/CDO_Hibernate_Store_Model_Relational_Mapping#Generating_the_mapping.2C_manually_changing_it_and_then_use_the_manual_mapping"
 * >this</a> wiki page for more information.
 * <p/>
 * The generated hbm is printed to the console. For the rest see the inline comments.
 * <p/>
 * Note that this method uses CDO server side plugins and Teneo. This results in two extra dependencies in this plugin:
 * <ul>
 * <li>org.eclipse.emf.teno</li>
 * <li>org.eclipse.emf.cdo.server.hibernate.teneo</li>
 * <li>org.apache.commons.logging</li>
 * </ul>
 * The first two dependencies normally do not exist on a client side application.
 * 
 * @author Martin Taal
 */
public class GenerateHBMTest extends TestCase
{

  public void testGenerateHBM()
  {
    // create a properties
    final Properties props = new Properties();
    // this property should normally be set like this:
    props.put(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT, "PERSIST,MERGE"); //$NON-NLS-1$

    // add some demo properties
    props.put(PersistenceOptions.INHERITANCE_MAPPING, "JOINED"); //$NON-NLS-1$
    props.put(PersistenceOptions.ADD_INDEX_FOR_FOREIGN_KEY, "true"); //$NON-NLS-1$

    // get the epackages
    final EPackage[] ePackages = new EPackage[] { CompanyPackage.eINSTANCE };

    // generate the mapping
    final CDOMappingGenerator mappingGenerator = new CDOMappingGenerator();
    final String mapping = mappingGenerator.generateMapping(ePackages, props);

    // show it somewhere....
    // then store the hbm somewhere in a file and change it manually
    IOUtil.ERR().println(mapping);
  }
}

Back to the top