You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by yokenji <ke...@gmail.com> on 2009/10/15 14:15:45 UTC

Set ForeignKey

When I do an export with the MappingTools to a sql file I cant' see my
Foreignkeys how does it come?
My persistenceFile looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="RackManagerEJB" transaction-type="RESOURCE_LOCAL">
	
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<class>entities.Element</class>
		<class>entities.Property</class>
    <exclude-unlisted-classes />
		<properties>
			<property name="openjpa.ConnectionURL"
value="jdbc:mysql://localhost:3306/rackmanagerdb" />
			<property name="openjpa.ConnectionDriverName"
value="com.mysql.jdbc.Driver" />
      <property name="openjpa.ConnectionUserName" value="root" />
      <property name="openjpa.ConnectionPassword" value="root" />
      <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
      <property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
      <property name="openjpa.jdbc.DBDictionary" value="mysql"/>
		</properties>
	</persistence-unit>
</persistence>


My Element entity like this:

@Entity
@Table(name="Element")
public class Element implements Serializable {

  private static final long serialVersionUID = 8689013550523480947L;

  public Element() {    
  }
  
  private String        id;
  private String        name;
  private String        description;
  private Set<Property> properties;
  private Element       parentElement;
  private Set<Element>  childElements;

  @Id
  public String getId() {
    return id;
  }
  
  public void setId(String id) {
    this.id = id;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public String getDescription() {
    return description;
  }
  
  public void setDescription(String description) {
    this.description = description;
  }
  
  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER,
mappedBy="element")
  public Set<Property> getProperties() {
    return properties;
  }
  
  public void setProperties(Set<Property> properties) {
    this.properties = properties;
  }
  
  @ManyToOne(optional=true)
  @ForeignKey(name="FK_parent_element")
  @JoinColumn(name="parent_Element", referencedColumnName="id")
  public Element getParentElement() {
    return parentElement;
  }
  
  public void setParentElement(Element parentElement) {
    this.parentElement = parentElement;
  }
  
  @OneToMany(mappedBy="parentElement")
  public Set<Element> getChildElements() {
    return childElements;
  }
  
  public void setChildElements(Element childElement) {
    this.childElements.add(childElement);
  }
  
}


my property like this:

@Entity
@Table(name="Property")
public class Property implements Serializable {

  private static final long serialVersionUID = -6988886476498387460L;

  public Property() {    
  }
  
  private int     id;
  private String  property;
  private String  value;
  private Element element;
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  public int getId() {
    return id;
  }
  
  public void setId(int id) {
    this.id = id;
  }
  
  public String getProperty() {
    return property;
  }
  
  public void setProperty(String property) {
    this.property = property;
  }
  
  public String getValue() {
    return value;
  }
  
  public void setValue(String value) {
    this.value = value;
  }

  @ManyToOne(fetch=FetchType.LAZY)
  @ForeignKey(name="FK_property_element")
  @JoinColumn(name="element", nullable=false)
  public Element getElement() {
    return element;
  }

  public void setElement(Element element) {
    this.element = element;
  }
    
}

my SQL output file:
CREATE TABLE Element (id VARCHAR(255) NOT NULL, description VARCHAR(255),
name VARCHAR(255), parent_Element VARCHAR(255), PRIMARY KEY (id)) TYPE =
innodb;
CREATE TABLE OPENJPA_SEQUENCE_TABLE (ID TINYINT NOT NULL, SEQUENCE_VALUE
BIGINT, PRIMARY KEY (ID)) TYPE = innodb;
CREATE TABLE Property (id INTEGER NOT NULL, property VARCHAR(255), value
VARCHAR(255), element VARCHAR(255) NOT NULL, PRIMARY KEY (id)) TYPE =
innodb;
CREATE INDEX I_ELEMENT_PARENTELEMENT ON Element (parent_Element);
CREATE INDEX I_PROPRTY_ELEMENT ON Property (element);

Did I forgot some thing to configurate or is the foreign key not supported?

Greets K

-- 
View this message in context: http://n2.nabble.com/Set-ForeignKey-tp3828908p3828908.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Set ForeignKey

Posted by yokenji <ke...@gmail.com>.
I found the solution for my problem, maybe intrested for other people!

Why OpenJPA is not creating foreign key constraints on the database tables?

By default, OpenJPA does not create foreign key constraints on new tables
that gets created according to O-R mapping annotation/descriptors. You can
change this default behavior via following configuration property

 
<property name="openjpa.jdbc.MappingDefaults"
value="ForeignKeyDeleteAction=restrict,
JoinForeignKeyDeleteAction=restrict"/>


Greets K,


Daryl Stultz wrote:
> 
> On Thu, Oct 15, 2009 at 8:50 AM, yokenji <ke...@gmail.com> wrote:
> 
>>
>> Nothing,
>>
>> :(
>>
>> Sorry, I'm afraid I don't know. I use the command line call rather than
>> Ant
> because the Ant task in 1.2.1 is broken when enums are used. You might try
> it from the command line.
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/Set-ForeignKey-tp3828908p3829831.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Set ForeignKey

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Oct 15, 2009 at 8:50 AM, yokenji <ke...@gmail.com> wrote:

>
> Nothing,
>
> :(
>
> Sorry, I'm afraid I don't know. I use the command line call rather than Ant
because the Ant task in 1.2.1 is broken when enums are used. You might try
it from the command line.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Set ForeignKey

Posted by yokenji <ke...@gmail.com>.
Nothing,

:(

Daryl Stultz wrote:
> 
> On Thu, Oct 15, 2009 at 8:35 AM, yokenji <ke...@gmail.com> wrote:
> 
>>
>> HI, I did but no changements: here's my ant file the command I use is ant
>> schema2file
>>    <mappingtool action="buildSchema" foreignkeys="true">
>>
> 
> Hmmm, try foreignKeys instead of foreignkeys.
> 
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/Set-ForeignKey-tp3828908p3829083.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Set ForeignKey

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Oct 15, 2009 at 8:35 AM, yokenji <ke...@gmail.com> wrote:

>
> HI, I did but no changements: here's my ant file the command I use is ant
> schema2file
>    <mappingtool action="buildSchema" foreignkeys="true">
>

Hmmm, try foreignKeys instead of foreignkeys.


-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Set ForeignKey

Posted by yokenji <ke...@gmail.com>.
HI, I did but no changements: here's my ant file the command I use is ant
schema2file

<?xml version="1.0" encoding="UTF-8"?>
<project name="openJPATools" default="help" basedir=".">
  <!-- Properties -->
  <property name="src.dir" value="${basedir}/ejbModule" />
  <property name="bld.dir" value="${basedir}/build" />
  <property name="mdl.dir" value="${bld.dir}/model" />
  <property name="lib.dir" value="${basedir}/lib" />
  
  <!-- Define the mappingTool task. --> 
  <property name="mappingTool"
value="org.apache.openjpa.jdbc.ant.MappingToolTask" />
  
  <!-- Define the reserveMappingTool task. -->  
  <property name="reverseMappingTool"
value="org.apache.openjpa.jdbc.ant.ReverseMappingToolTask" />
    
  <!-- Define the schemaTool task. -->  
  <property name="schemaTool"
value="org.apache.openjpa.jdbc.ant.SchemaToolTask" />
  
  <!-- Define the persistence file. -->
  <property name="persistence"
value="${basedir}/ejbModule/META-INF/persistence.xml" />
    
  <!-- Encoding language -->
  <property name="encode" value="UTF-8" />
    
  <!-- Debuggin (true/false) -->  
  <property name="debug" value="true" />
    
  <!-- Debug level (lines|vars|source) more then one keyword can be
specified. -->  
  <property name="debugLevel" value="lines,vars,source" />
    
  <!-- Define the filename where the create schema will be stored. -->    
  <property name="createSql" value="${basedir}/create.sql" />
  
  <!-- Define the filename where the update schema will be stored. -->
  <property name="updateSql" value="${basedir}/update.sql" />  
    
  <!-- Show possible options. -->    
  <target name="help">
    <echo>Available targets:
    - clean
    - build
    - schema2file
    - schema2db
    - upschema2file
    - upschema2db</echo>
  </target>  
    
  <!-- Initialization, Things to do before starting. -->  
  <target name="init">
    <mkdir dir="${mdl.dir}"/>
  </target>  
    
  <!-- Define the library path. -->  
  <path id="classpath">
    <fileset dir="${basedir}">
      <include name="**/*.jar" />
    </fileset>
  </path>
  
  <!-- Build all files. -->  
  <target name="build" depends="init" description="Generates java classes">
    <javac srcdir="${src.dir}" destdir="${mdl.dir}" debug="${debug}"
debuglevel="${debugLevel}">
      <classpath refid="classpath" />
    </javac>      
  </target>

  <!-- Export the Schema to a File. -->  
  <target name="schema2file" depends="build" description="Generates and
export the schema to a file.">
    <taskdef name="mappingtool" classname="${mappingTool}">
      <classpath refid="classpath" />
    </taskdef>
      
    <mappingtool action="buildSchema" schemaaction="build"
sqlFile="${createSql}" foreignkeys="true">
      <config propertiesFile="${persistence}" />
      <classpath>
        <pathelement location="${mdl.dir}" />
      </classpath>
    </mappingtool>
  </target>

  <!-- Export the Schema to the Database. -->
  <target name="schema2db" depends="build" description="Generates and export
the schema to the database.">
    <taskdef name="mappingtool" classname="${mappingTool}">
      <classpath refid="classpath" />
    </taskdef>
      
    <mappingtool action="buildSchema" foreignkeys="true">
      <config propertiesFile="${persistence}" />
      <classpath>
        <pathelement location="${mdl.dir}" />
      </classpath>
    </mappingtool>  
  </target>  
       
  <!-- Export the updated Schema to a file. -->  
  <target name="upschema2file" depends="build" description="Update and
export the schema to a file.">
    <taskdef name="mappingtool" classname="${mappingTool}">
      <classpath refid="classpath" />
    </taskdef>

    <mappingtool sqlfile="${updateSql}">
      <config propertiesFile="${persistence}" />
      <classpath>
        <pathelement location="${mdl.dir}" />
      </classpath>
    </mappingtool>   
  </target>
    
  <!--Export the updated Schema to the database. -->  
  <target name="upschema2db" depends="build" description="Update and export
the schema to the database.">
    <taskdef name="mappingtool" classname="${mappingTool}">
      <classpath refid="classpath" />
    </taskdef>
    
    <mappingtool action="buildSchema">
      <config propertiesFile="${persistence}" />
    
      <classpath> 
        <pathelement location="${mdl.dir}"/>
      </classpath>  
    </mappingtool>  
  </target>
    
  <!-- Cleanup the build directory. -->
  <target name="clean">
    <delete dir="${bld.dir}" />
  </target>
    
</project>
  



Daryl Stultz wrote:
> 
> On Thu, Oct 15, 2009 at 8:15 AM, yokenji <ke...@gmail.com> wrote:
> 
>>
>> When I do an export with the MappingTools to a sql file I cant' see my
>> Foreignkeys how does it come?
>>
> 
> If you are using Ant, add foreignKeys="true" to <mappingtool>. If using
> the
> command line tool, include -foreignKeys true.
> 
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/Set-ForeignKey-tp3828908p3829022.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Set ForeignKey

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Oct 15, 2009 at 8:15 AM, yokenji <ke...@gmail.com> wrote:

>
> When I do an export with the MappingTools to a sql file I cant' see my
> Foreignkeys how does it come?
>

If you are using Ant, add foreignKeys="true" to <mappingtool>. If using the
command line tool, include -foreignKeys true.

--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com