You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by anand213 <ak...@bankofamerica.com> on 2012/12/06 16:00:25 UTC

Unique constraint annotations not generated by Reverse Mapping tool

Hi, 
I am trying to reverse map beans by directly connecting to H2 DB. Below is
the DB schema to reverse. It has couple of unique keys and a non-unique key.
These are read by the tool but no annotations generated for the same: 

CREATE SCHEMA TEST; 

CREATE TABLE TEST.EMPLOYEE_FOR_CACHE ( 
ID INT PRIMARY KEY, 
FNAME VARCHAR2(50), 
LNAME VARCHAR2(50), 
PROJECT VARCHAR2(10), 
DEPT VARCHAR2(50), 
LOCATION VARCHAR2(50) 
); 

-- Indexes: 
CREATE INDEX ON TEST.EMPLOYEE_FOR_CACHE(LNAME); 
CREATE UNIQUE INDEX ON TEST.EMPLOYEE_FOR_CACHE(FNAME, LNAME, LOCATION); 
CREATE UNIQUE INDEX ON TEST.EMPLOYEE_FOR_CACHE(DEPT, PROJECT); 


Below is the generated bean: 
@Entity 
@Table( name="EMPLOYEE_FOR_CACHE") 
public class EmployeeForCache { 
        @Basic 
        @Column(length=50) 
        private String dept; 

        @Basic 
        @Column(length=50) 
        private String fname; 

        @Id 
        private int id; 

        @Basic 
        @Column(length=50) 
        private String lname; 

        @Basic 
        @Column(length=50) 
        private String location; 

        @Basic 
        @Column(length=10) 
        private String project; 


        public EmployeeForCache() { 
        } 

        public EmployeeForCache(int id) { 
                this.id = id; 
        } 

        public String getDept() { 
                return dept; 
        } 

        public void setDept(String dept) { 
                this.dept = dept; 
        } 

        public String getFname() { 
                return fname; 
        } 

        public void setFname(String fname) { 
                this.fname = fname; 
        } 

        public int getId() { 
                return id; 
        } 

        public void setId(int id) { 
                this.id = id; 
        } 

        public String getLname() { 
                return lname; 
        } 

        public void setLname(String lname) { 
                this.lname = lname; 
        } 

        public String getLocation() { 
                return location; 
        } 

        public void setLocation(String location) { 
                this.location = location; 
        } 

        public String getProject() { 
                return project; 
        } 

        public void setProject(String project) { 
                this.project = project; 
        } 
} 



Below are the logs: 

94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (FNAME)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (LNAME)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (LOCATION)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_14C" on table "TEST.EMPLOYEE_FOR_CACHE (DEPT)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_14C" on table "TEST.EMPLOYEE_FOR_CACHE (PROJECT)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "PRIMARY_KEY_1" on table "TEST.EMPLOYEE_FOR_CACHE (ID)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
existing index "INDEX_1" on table "TEST.EMPLOYEE_FOR_CACHE (LNAME)". 
94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Reading
foreign keys for schema name "TEST", table name "TEST.EMPLOYEE_FOR_CACHE". 
109  cut-jpa-bean-generator  INFO   [main] openjpa.Tool - ReverseMappingTool
: generating classes. 
107259  cut-jpa-bean-generator  INFO   [main] openjpa.Tool - Generating
annotations. 
110368  cut-jpa-bean-generator  INFO   [main] openjpa.Tool - Writing
generated class source code. 



I am using below reverse properties: 
-pkg, com.bofa.cut.jpa.entity.bean, -directory, ./scr/test/java, -log,
'DefaultLevel=WARN, Tool=INFO, SQL=TRACE,Schema=TRACE', -metadata, none,
-useGenericCollections, true, -annotations, true, -connectionURL, ..... 

OPen JPA version: 2.2.0

Thanks and Regards, 
AKumar



--
View this message in context: http://openjpa.208410.n2.nabble.com/Unique-constraint-annotations-not-generated-by-Reverse-Mapping-tool-tp7582055.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Unique constraint annotations not generated by Reverse Mapping tool

Posted by Kevin Sutter <kw...@gmail.com>.
Ahh, thanks for the clarification.  Although Unique Indexes and Unique
Constraints are very similar in function, my guess is OpenJPA is only
recognizing Unique Constraints in the schema.  Then again, it may be that
the Unique Constraints are not recognized at all during the Reverse Mapping
process...  Sorry, I just haven't had direct experience with this use of
the Reverse Mapping.

You could try using Unique Constraints in your schema instead of Unique
Indexes to see if it makes a difference.  Otherwise, you might have to
resort to hand-modifying the Entity java code that is generated.  Opening a
JIRA for this capability would also be a good idea.

Thanks,
Kevin

On Fri, Dec 7, 2012 at 12:29 PM, anand213 <ak...@bankofamerica.com>wrote:

> Thanks for the response Kevin.
>
> I was looking for @UniqueConstraint annotation in generated files and the
> tool is reading the unique indexes so i expected it to put annotation for
> the same.
>
> @Table(name = "EMPLOYEE_FOR_CACHE", uniqueConstraints = {
> @UniqueConstraint(columnNames = { "dept", "project" }),
>                 @UniqueConstraint(columnNames = { "fname", "lname",
> "location" }) })
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Unique-constraint-annotations-not-generated-by-Reverse-Mapping-tool-tp7582055p7582061.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Unique constraint annotations not generated by Reverse Mapping tool

Posted by anand213 <ak...@bankofamerica.com>.
Thanks for the response Kevin. 

I was looking for @UniqueConstraint annotation in generated files and the
tool is reading the unique indexes so i expected it to put annotation for
the same.

@Table(name = "EMPLOYEE_FOR_CACHE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "dept", "project" }),
		@UniqueConstraint(columnNames = { "fname", "lname", "location" }) })



--
View this message in context: http://openjpa.208410.n2.nabble.com/Unique-constraint-annotations-not-generated-by-Reverse-Mapping-tool-tp7582055p7582061.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Unique constraint annotations not generated by Reverse Mapping tool

Posted by Kevin Sutter <kw...@gmail.com>.
Hi akumar,
What exactly were you expecting from the reverse mapping tool?  Were you
expecting that the @org.apache.openjpa.persistence.jdbc.Index annotations
to be inserted into the generated java files?  Or, were you expecting
something else?  You mentioned unique and non-unique keys...  But, the
primary key is detected correctly with the @Id annotation generation.  I'm
guessing you meant unique and non-unique indexes...

If you were expecting the @Index annotation, per the documentation, this
annotation was really meant for the other direction.  That is, it's used as
a hint for OpenJPA when generating the database schema [1]:

"..OpenJPA uses index information during schema generation to index the
proper columns."

And, even the Reverse Mapping tool is not meant to be the end-all game [2]:

"..Examine the generated class, metadata, and mapping information, and
modify it as necessary. Remember that the reverse mapping tool only
provides a starting point, and you are free to make whatever modifications
you like to the code it generates."

Not saying that this process couldn't be enhanced as a feature request, but
I don't think there's a bug in the processing.

Good luck,
Kevin


[1]
http://openjpa.apache.org/builds/latest/docs/docbook/manual.html#ref_guide_mapping_jpa_constraints
[2]
http://openjpa.apache.org/builds/latest/docs/docbook/manual.html#ref_guide_pc_reverse
<http://openjpa.apache.org/builds/latest/docs/javadoc/org/apache/openjpa/persistence/jdbc/Index.html>
On Thu, Dec 6, 2012 at 9:00 AM, anand213 <ak...@bankofamerica.com>wrote:

> Hi,
> I am trying to reverse map beans by directly connecting to H2 DB. Below is
> the DB schema to reverse. It has couple of unique keys and a non-unique
> key.
> These are read by the tool but no annotations generated for the same:
>
> CREATE SCHEMA TEST;
>
> CREATE TABLE TEST.EMPLOYEE_FOR_CACHE (
> ID INT PRIMARY KEY,
> FNAME VARCHAR2(50),
> LNAME VARCHAR2(50),
> PROJECT VARCHAR2(10),
> DEPT VARCHAR2(50),
> LOCATION VARCHAR2(50)
> );
>
> -- Indexes:
> CREATE INDEX ON TEST.EMPLOYEE_FOR_CACHE(LNAME);
> CREATE UNIQUE INDEX ON TEST.EMPLOYEE_FOR_CACHE(FNAME, LNAME, LOCATION);
> CREATE UNIQUE INDEX ON TEST.EMPLOYEE_FOR_CACHE(DEPT, PROJECT);
>
>
> Below is the generated bean:
> @Entity
> @Table( name="EMPLOYEE_FOR_CACHE")
> public class EmployeeForCache {
>         @Basic
>         @Column(length=50)
>         private String dept;
>
>         @Basic
>         @Column(length=50)
>         private String fname;
>
>         @Id
>         private int id;
>
>         @Basic
>         @Column(length=50)
>         private String lname;
>
>         @Basic
>         @Column(length=50)
>         private String location;
>
>         @Basic
>         @Column(length=10)
>         private String project;
>
>
>         public EmployeeForCache() {
>         }
>
>         public EmployeeForCache(int id) {
>                 this.id = id;
>         }
>
>         public String getDept() {
>                 return dept;
>         }
>
>         public void setDept(String dept) {
>                 this.dept = dept;
>         }
>
>         public String getFname() {
>                 return fname;
>         }
>
>         public void setFname(String fname) {
>                 this.fname = fname;
>         }
>
>         public int getId() {
>                 return id;
>         }
>
>         public void setId(int id) {
>                 this.id = id;
>         }
>
>         public String getLname() {
>                 return lname;
>         }
>
>         public void setLname(String lname) {
>                 this.lname = lname;
>         }
>
>         public String getLocation() {
>                 return location;
>         }
>
>         public void setLocation(String location) {
>                 this.location = location;
>         }
>
>         public String getProject() {
>                 return project;
>         }
>
>         public void setProject(String project) {
>                 this.project = project;
>         }
> }
>
>
>
> Below are the logs:
>
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (FNAME)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (LNAME)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_14" on table "TEST.EMPLOYEE_FOR_CACHE (LOCATION)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_14C" on table "TEST.EMPLOYEE_FOR_CACHE (DEPT)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_14C" on table "TEST.EMPLOYEE_FOR_CACHE (PROJECT)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "PRIMARY_KEY_1" on table "TEST.EMPLOYEE_FOR_CACHE (ID)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Found
> existing index "INDEX_1" on table "TEST.EMPLOYEE_FOR_CACHE (LNAME)".
> 94  cut-jpa-bean-generator  TRACE  [main] openjpa.jdbc.Schema - Reading
> foreign keys for schema name "TEST", table name "TEST.EMPLOYEE_FOR_CACHE".
> 109  cut-jpa-bean-generator  INFO   [main] openjpa.Tool -
> ReverseMappingTool
> : generating classes.
> 107259  cut-jpa-bean-generator  INFO   [main] openjpa.Tool - Generating
> annotations.
> 110368  cut-jpa-bean-generator  INFO   [main] openjpa.Tool - Writing
> generated class source code.
>
>
>
> I am using below reverse properties:
> -pkg, com.bofa.cut.jpa.entity.bean, -directory, ./scr/test/java, -log,
> 'DefaultLevel=WARN, Tool=INFO, SQL=TRACE,Schema=TRACE', -metadata, none,
> -useGenericCollections, true, -annotations, true, -connectionURL, .....
>
> OPen JPA version: 2.2.0
>
> Thanks and Regards,
> AKumar
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Unique-constraint-annotations-not-generated-by-Reverse-Mapping-tool-tp7582055.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>