Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added foreign keys, fixed db structure.

  • Loading branch information...
commit 551da21a47fc6b7e6f4eef5a68a34b100057f1f0 1 parent 94cf751
meri authored
View
23 src/main/java/org/meri/jpa/relationships/entities/onetomany/MapOneToManyInverse.java
@@ -1,24 +1,15 @@
package org.meri.jpa.relationships.entities.onetomany;
-import java.util.Map;
-
import javax.persistence.Entity;
import javax.persistence.Id;
-import javax.persistence.MapKey;
-import javax.persistence.OneToMany;
-
-import org.meri.jpa.relationships.entities.onetomany_manytoone.MapOwner;
@Entity
public class MapOneToManyInverse {
@Id
private long id;
-
- @OneToMany
- @MapKey(name="mapKey")
- private Map<String, MapOwner> owners;
-
+ private String mapKey;
+
public long getId() {
return id;
}
@@ -27,12 +18,12 @@ public void setId(long id) {
this.id = id;
}
- public Map<String, MapOwner> getOwners() {
- return owners;
+ public String getMapKey() {
+ return mapKey;
}
- public void setOwners(Map<String, MapOwner> owners) {
- this.owners = owners;
+ public void setMapKey(String mapKey) {
+ this.mapKey = mapKey;
}
-
+
}
View
21 src/main/java/org/meri/jpa/relationships/entities/onetomany/MapOneToManyOwner.java
@@ -1,15 +1,22 @@
package org.meri.jpa.relationships.entities.onetomany;
+import java.util.Map;
+
import javax.persistence.Entity;
import javax.persistence.Id;
+import javax.persistence.MapKey;
+import javax.persistence.OneToMany;
@Entity
public class MapOneToManyOwner {
@Id
private long id;
- private String mapKey;
-
+
+ @OneToMany
+ @MapKey(name="mapKey")
+ private Map<String, MapOneToManyInverse> inverses;
+
public long getId() {
return id;
}
@@ -18,12 +25,12 @@ public void setId(long id) {
this.id = id;
}
- public String getMapKey() {
- return mapKey;
+ public Map<String, MapOneToManyInverse> getInverses() {
+ return inverses;
}
- public void setMapKey(String mapKey) {
- this.mapKey = mapKey;
+ public void setInverses(Map<String, MapOneToManyInverse> owners) {
+ this.inverses = owners;
}
-
+
}
View
3  src/main/resources/META-INF/persistence.xml
@@ -55,6 +55,8 @@
<class>org.meri.jpa.relationships.entities.manytoone.UnidirectionalManyToOneInverse</class>
<class>org.meri.jpa.relationships.entities.onetomany.OneToManyOwner</class>
<class>org.meri.jpa.relationships.entities.onetomany.OneToManyInverse</class>
+ <class>org.meri.jpa.relationships.entities.onetomany.MapOneToManyOwner</class>
+ <class>org.meri.jpa.relationships.entities.onetomany.MapOneToManyInverse</class>
<class>org.meri.jpa.relationships.entities.onetomany.ColumnOneToManyOwner</class>
<class>org.meri.jpa.relationships.entities.onetomany.ColumnOneToManyInverse</class>
<class>org.meri.jpa.relationships.entities.onetomany.TableOneToManyOwner</class>
@@ -70,7 +72,6 @@
<class>org.meri.jpa.relationships.entities.manytomany.TableMtmOwner</class>
<class>org.meri.jpa.relationships.entities.manytomany.TableMtmInverse</class>
-
<!-- other properties -->
<properties>
<!-- Do not use in production, this will generate a lot of output. -->
View
22 src/test/java/org/meri/jpa/relationships/OneToManyTestCase.java
@@ -4,12 +4,15 @@
import java.util.Arrays;
import java.util.Collection;
+import java.util.Map;
import javax.persistence.EntityManager;
import org.junit.Test;
import org.meri.jpa.relationships.entities.onetomany.ColumnOneToManyInverse;
import org.meri.jpa.relationships.entities.onetomany.ColumnOneToManyOwner;
+import org.meri.jpa.relationships.entities.onetomany.MapOneToManyInverse;
+import org.meri.jpa.relationships.entities.onetomany.MapOneToManyOwner;
import org.meri.jpa.relationships.entities.onetomany.OneToManyInverse;
import org.meri.jpa.relationships.entities.onetomany.OneToManyOwner;
import org.meri.jpa.relationships.entities.onetomany.OrphanOneToManyInverse;
@@ -26,8 +29,11 @@ protected String getInitialChangeLog() {
return CHANGELOG_LOCATION;
}
+ /**
+ * Basic test, load some data and check them.
+ */
@Test
- public void unidirectionalOneToMany() {
+ public void basicLoad() {
EntityManager em = factory.createEntityManager();
OneToManyOwner owner = em.find(OneToManyOwner.class, 1);
@@ -39,6 +45,20 @@ public void unidirectionalOneToMany() {
}
@Test
+ public void basicMap() {
+ EntityManager em = factory.createEntityManager();
+
+ MapOneToManyOwner owner = em.find(MapOneToManyOwner.class, 1);
+ MapOneToManyInverse inverse = em.find(MapOneToManyInverse.class, 5);
+
+ // mapKey property is a key to the map
+ Map<String, MapOneToManyInverse> inverses = owner.getInverses();
+ assertEquals(inverse, inverses.get(inverse.getMapKey()));
+
+ em.close();
+ }
+
+ @Test
public void tableOneToMany() {
EntityManager em = factory.createEntityManager();
View
49 src/test/java/org/meri/jpa/relationships/db.onetomany.changelog.xml
@@ -4,7 +4,7 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
- <changeSet id="13" author="meri">
+ <changeSet id="1" author="meri">
<createTable tableName="OneToManyOwner">
<column name="id" type="integer">
<constraints primaryKey="true" nullable="false" />
@@ -32,7 +32,7 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
referencedTableName="OneToManyOwner" referencedColumnNames="id" />
</changeSet>
- <changeSet id="14" author="meri">
+ <changeSet id="2" author="meri">
<insert tableName="OneToManyInverse">
<column name="id" valueNumeric="5" />
</insert>
@@ -43,8 +43,52 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<column name="inverses_id" valueNumeric="5" />
<column name="OneToManyOwner_id" valueNumeric="1" />
</insert>
+
+ </changeSet>
+
+ <changeSet id="3" author="meri">
+ <createTable tableName="MapOneToManyOwner">
+ <column name="id" type="integer">
+ <constraints primaryKey="true" nullable="false" />
+ </column>
+ </createTable>
+ <createTable tableName="MapOneToManyInverse">
+ <column name="id" type="integer">
+ <constraints primaryKey="true" nullable="false" />
+ </column>
+ <column name="mapkey" type="varchar(1500)" />
+ </createTable>
+ <createTable tableName="MapOneToManyOwner_inverses">
+ <column name="MapOneToManyOwner_id" type="integer">
+ <constraints nullable="false" />
+ </column>
+ <column name="element_id" type="integer" >
+ <constraints nullable="false" unique="true" />
+ </column>
+ </createTable>
+
+ <addForeignKeyConstraint constraintName="fk_MapOneToManyInverse"
+ baseTableName="MapOneToManyOwner_inverses" baseColumnNames="element_id"
+ referencedTableName="MapOneToManyInverse" referencedColumnNames="id" />
+ <addForeignKeyConstraint constraintName="fk_MapOneToManyOwner"
+ baseTableName="MapOneToManyOwner_inverses" baseColumnNames="MapOneToManyOwner_id"
+ referencedTableName="MapOneToManyOwner" referencedColumnNames="id" />
</changeSet>
+ <changeSet id="4" author="meri">
+ <insert tableName="MapOneToManyInverse">
+ <column name="id" valueNumeric="5" />
+ <column name="mapkey" value="five" />
+ </insert>
+ <insert tableName="MapOneToManyOwner">
+ <column name="id" valueNumeric="1" />
+ </insert>
+ <insert tableName="MapOneToManyOwner_inverses">
+ <column name="MapOneToManyOwner_id" valueNumeric="1" />
+ <column name="element_id" valueNumeric="5" />
+ </insert>
+ </changeSet>
+
<changeSet id="15" author="meri">
<createTable tableName="ColumnOneToManyOwner">
<column name="id" type="integer">
@@ -62,7 +106,6 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<constraints nullable="false" />
</column>
</createTable>
-
</changeSet>
<changeSet id="16" author="meri">
Please sign in to comment.
Something went wrong with that request. Please try again.