[ 
https://issues.apache.org/jira/browse/OPENJPA-399?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12533899
 ] 

Teresa Kan commented on OPENJPA-399:
------------------------------------

Example of the problem:
@Entity(name="Dog")
@Table(name="DOGTAB")

@IdClass(DogId.class)
public class Dog implements Serializable
        
{
        @Id
                     @GeneratedValue(strategy=GenerationType.AUTO)
        private int id2;
..
)

@Entity(name="Dog2")
@Table(name="DOGTAB")

@IdClass(DogId.class)
public class Dog2 implements Serializable
        
{
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id2;
..
)

Although the datasource uses different schema name, openjpa only created one 
table for DOG but not DOG2.


The current architecture based on the following assumption: 
Each PU has one JDBCConfiguration, each JDBCConfiguration has only one SeqValue 
and one TableJDBCSeq instance. These are singletons. During the 
JDBCConfiguration instantate phase, the TableJDBCSeq was created and setup the 
default schema. At this point, there was no correlation between the actual 
schema with the table and with the entity class. Each entity class has the 
SchemaGroup which contains all the schemas within this PU, but you can tell 
which schema is used for this entity. 

The ultimate solution is to change the JDBCConfiguration to know the entity and 
its schema info at the instantiation phase. Then makes the seqValue and 
TableJDBCSeq as singleton for each schema. I tried to figure out how to make it 
work but failed. I can't find any correlation between entity, schema and table 
name at the configuration instantiation phase.. ..

My current solution is  based on the current architecture and fix it in the 
TableJDBCSeq and NativeJDBCSeq. Since the TableJDBCSeq /NativeJDBCSeq is a 
singleton, so I put the logic to add the seq table name for each schema for 
this SchemaGroup.. Then during the Insert and Update the seq table, the table 
name will be fully quality with the schema name, so we can insert to the 
correct seq table.. 

There are some restrictions for using multiple schemas for the same table name 
that we need to document:

1) If multiple entities have the same table name,  then they must be qualified 
with the schema name either thru the annotation or xml mapping. If one of 
entity does not have a schema and one entity has a schema, it still fails when 
the application executes again.
2) If multiple entities have the generatedType.AUTO, SEQUENCE, TABLE for ID and 
using the same table name, then each entity must have the schema name.
3) For those entities which have its unique table name within the PU, then the 
schema name is not required even though the entity using the generatedType of 
AUTO, SEQUENCE and TABLE. -- this is today's implementation.

For the generatedType.SEQUENCE, if multiple entities use different scehmas for 
the same table and the sequence generator are the same, then only one sequence  
is created and is used for all the entities. However, if each entity has its 
own sequence generator, then each generator will have its own sequence. For 
example,
@Entity(name="Dog")
@Table(name="DOGTAB", schema="DB2ADMIN")

@IdClass(DogId.class)
public class Dog implements Serializable
        
{
        @Id
        @SequenceGenerator(name="myseq",sequenceName="order_seq2")
        @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="myseq")
        private int id2;
..
)

@Entity(name="Dog2")
@Table(name="DOGTAB", schema="TWC")

@IdClass(DogId.class)
public class Dog2 implements Serializable
        
{
        @Id
        @SequenceGenerator(name="mysequence",sequenceName="order_seq9")
        @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mysequence")
        private int id2;
..
)
The output will be like this:
executing prepstmnt 538058770 VALUES NEXTVAL FOR order_seq2
executing prepstmnt 1078214724 VALUES NEXTVAL FOR order_seq9

the id values are :
after find, dog1 id2  = 425 and dog name =helloDog
after find, dog11 id2  = 426 and dog name =helloDog1a
after find, dog3 id2  = 5 and dog name =helloDog2
after find, dog31 id2  = 6 and dog name =helloDog2a





> openjpa did not handle multiple schema names with same table name
> -----------------------------------------------------------------
>
>                 Key: OPENJPA-399
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-399
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jdbc
>    Affects Versions: 1.0.1
>         Environment: JDK1.5, OPENJPA verison 580425
>            Reporter: Teresa Kan
>
> Two entities have the same table name but with different schema, only one 
> table is created. In addition, when two entities use the generatedType.AUTO 
> for ID, only one OPENJPA_SEQUENCE-TABLE is created.
> The problem due to the SchemaGroup.findTable() which only looked for a table 
> name from all the schemas. Once the table was found in one of the schema then 
> it exited and assumed that the table existed. Same problem in the 
> TableJDBCSeq.addSchema().

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to