Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Partitioning with unidirectional associations

If you want to have a relationship that spans partitions, then you must
remove the foreign key constraint.

Otherwise, you need to ensure that both parent/child instances go to the
same partition.  If your child hashed on its foreign key column to its
parent's id, then they should have the same hash.  Or you could use another
partitioning policy that ensured both have the same partition.


Mike Key-3 wrote:
> 
> I think my question should be expanded a bit as I've run across a curious
> situation.  I'm trying to model something after the example found on:
> 
> http://wiki.eclipse.org/EclipseLink/Examples/JPA/Partitioning
> 
> I have a slightly more simplistic example.  My persistence unit looks
> like:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"; version="2.0">
>   <persistence-unit name="sample" transaction-type="RESOURCE_LOCAL">
>     <class>samples.model.EmailAddress</class>
>     <class>samples.model.Customer</class>
>     <properties>
>       <property name="javax.persistence.jdbc.driver"
>                 value="com.mysql.jdbc.Driver"/>
>       <property name="javax.persistence.jdbc.url"
>                 value="jdbc:mysql://localhost:3306/defaultdb"/>
>       <property name="javax.persistence.jdbc.user" value="defaultdb"/>
>       <property name="javax.persistence.jdbc.password" value="defaultdb"/>
> 
>       <!-- Configure default connection pool. -->
>       <property name="eclipselink.connection-pool.default.initial"
> value="1"/>
>       <property name="eclipselink.connection-pool.default.min"
> value="64"/>
>       <property name="eclipselink.connection-pool.default.max"
> value="64"/>
> 
>       <!-- Configure 2nd database connection pool. -->
>       <property name="eclipselink.connection-pool.node2.url"
>                 value="jdbc:mysql://localhost:3306/node2"/>
>       <property name="eclipselink.connection-pool.node2.user"
> value="node2"/>
>       <property name="eclipselink.connection-pool.node2.password"
>                 value="node2"/>
>       <property name="eclipselink.connection-pool.node2.initial"
> value="1"/>
>       <property name="eclipselink.connection-pool.node2.min" value="64"/>
>       <property name="eclipselink.connection-pool.node2.max" value="64"/>
> 
>       <!-- Default partioning to replication to allow DDL to be sent to
> all nodes -->
>       <property name="eclipselink.partitioning" value="Replicate"/>
>     </properties>
>   </persistence-unit>
> </persistence>
> 
> Customer has the following partitions and OneToMany to EmailAddress:
> 
> @Entity
> @Table(name = "customer")
> @UnionPartitioning(
>         name = "UnionPartitioningAllNodes",
>         replicateWrites = true,
>         connectionPools = {"default", "node2"})
> @ReplicationPartitioning(name="Replicate",
> connectionPools={"default","node2"})
> @HashPartitioning(name="HashPartitionByCustomerNamespace",
> partitionColumn=@Column(name="NAMESPACE"),
> connectionPools={"default","node2"}, unionUnpartitionableQueries = true)
> @Partitioned("HashPartitionByCustomerNamespace")
> public class Customer implements Serializable {
> 
>     @Id
>     @GeneratedValue(strategy = GenerationType.AUTO)
>     private Long id;
> 
>     private int namespace = 0;
> 
>     private String firstName;
> 
>     private String lastName;
> 
>     @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
>     private List<EmailAddress> emailAddresses = new
> ArrayList<EmailAddress>();
> 
> 
> EmailAddress then has:
> 
> @Entity
> @Table(name = "email_address")
> @HashPartitioning(name="HashPartitionByEmailNamespace",
> partitionColumn=@Column(name="NAMESPACE"),
> connectionPools={"default","node2"}, unionUnpartitionableQueries = true)
> @Partitioned("HashPartitionByEmailNamespace")
> public class EmailAddress {
> 
>     @Id
>     @GeneratedValue(strategy = GenerationType.AUTO)
>     private Long id;
> 
>     private String address;
> 
>     private String type;
> 
>     private int namespace = 0;
> 
>     @ManyToOne
>     private Customer customer;
> 
> I have a simple test that just creates a new Customer and adds an email
> address and then I try to persist this.
> 
> The hash passes in simple 0 or 1 in namespace to determine which datastore
> to save the record in.  If I try to persist Customer in one datastore and
> EmailAddress in the other, I get the following:
> 
> Caused by:
> com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
> Cannot add or update a child row: a foreign key constraint fails
> (`default`.`email_address`, CONSTRAINT `FK_email_address_CUSTOMER_ID`
> FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`ID`))
> 
> I would assume this would work as the sample code at the link above seems
> to suggest.  Is there something I am doing wrong here that is obvious?  I
> should note I am creating the entity manager via Spring as follows:
> 
>  <bean id="entityManagerFactory"
>        
> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>     <property name="persistenceUnitName" value="sample"/>
>     <property name="persistenceProvider"
> ref="eclipseLinkPersistenceProvider"/>
>     <property name="jpaVendorAdapter">
>       <bean
>          
> class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
>         <property name="databasePlatform"
>                  
> value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
>         <property name="showSql" value="true"/>
>         <property name="generateDdl" value="true"/>
>       </bean>
>     </property>
>   </bean>
> 
> Thanks in advance for the help.
> 
> Mike Key
> 
>   
> On Mar 22, 2012, at 1:59 PM, Mike Key wrote:
> 
>> Forgive me if this has been answered here already.  I am setting up some
>> partitioning schemes in Eclipselink 2.3.2 with JPA.  It would seem my
>> simple example will only work for One-To-Many relationships if the
>> association is bi-directional.  Is this by design?  It seems to make
>> sense logically that it is, however I am wondering if uni-directional
>> associations are supported in any way for partitioning?
>> 
>> Thanks for any help.
>> 
>> 
> 
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.eclipse.org/forums/index.php?t=thread&frm_id=111&S=1b00bfd151289b297688823a00683aca
EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/Partitioning-with-unidirectional-associations-tp33555782p33558028.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top