diff --git a/build.xml b/build.xml index e598621f..66b69abf 100644 --- a/build.xml +++ b/build.xml @@ -61,7 +61,6 @@ - diff --git a/depends-incl.xml b/depends-incl.xml index c8eb01b0..b0894727 100644 --- a/depends-incl.xml +++ b/depends-incl.xml @@ -31,10 +31,6 @@ classname="javax.xml.parsers.SAXParser" classpathref="classpath"/> JAXP: ${jaxp.present} - - Java Persistence: ${javax.persistence.present} - ---------------------------------------------- Jakarta libraries - http://jakarta.apache.org/ @@ -88,14 +84,6 @@ com.samskivert.jdbc: ${build.jdbc} - - - - - - - com.samskivert.jdbc.depot: ${build.depot} - diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 4d3009d8..acd9cb6f 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -37,10 +37,11 @@ import java.util.HashSet; import java.util.Set; import java.util.logging.Level; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.TableGenerator; -import javax.persistence.Transient; +import com.samskivert.jdbc.depot.annotation.Computed; +import com.samskivert.jdbc.depot.annotation.GeneratedValue; +import com.samskivert.jdbc.depot.annotation.Id; +import com.samskivert.jdbc.depot.annotation.TableGenerator; +import com.samskivert.jdbc.depot.annotation.Transient; import com.samskivert.jdbc.JDBCUtil; import com.samskivert.jdbc.depot.clause.Where; diff --git a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java index c072c917..bb3dd7b4 100644 --- a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java @@ -31,10 +31,11 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; -import javax.persistence.Column; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import com.samskivert.jdbc.depot.annotation.Column; +import com.samskivert.jdbc.depot.annotation.Computed; +import com.samskivert.jdbc.depot.annotation.GeneratedValue; +import com.samskivert.jdbc.depot.annotation.GenerationType; +import com.samskivert.jdbc.depot.annotation.Id; import com.samskivert.util.StringUtil; diff --git a/src/java/com/samskivert/jdbc/depot/PersistenceContext.java b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java index 3bb26f88..fa543ada 100644 --- a/src/java/com/samskivert/jdbc/depot/PersistenceContext.java +++ b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java @@ -22,7 +22,7 @@ package com.samskivert.jdbc.depot; import java.util.HashMap; -import javax.persistence.TableGenerator; +import com.samskivert.jdbc.depot.annotation.TableGenerator; import java.sql.Connection; import java.sql.SQLException; diff --git a/src/java/com/samskivert/jdbc/depot/Query.java b/src/java/com/samskivert/jdbc/depot/Query.java index 608ec83d..f368a582 100644 --- a/src/java/com/samskivert/jdbc/depot/Query.java +++ b/src/java/com/samskivert/jdbc/depot/Query.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Set; import com.samskivert.io.PersistenceException; +import com.samskivert.jdbc.depot.annotation.Computed; import com.samskivert.jdbc.depot.clause.FieldOverride; import com.samskivert.jdbc.depot.clause.ForUpdate; import com.samskivert.jdbc.depot.clause.FromOverride; diff --git a/src/java/com/samskivert/jdbc/depot/TableKeyGenerator.java b/src/java/com/samskivert/jdbc/depot/TableKeyGenerator.java index f08ae38a..fc3fdfd1 100644 --- a/src/java/com/samskivert/jdbc/depot/TableKeyGenerator.java +++ b/src/java/com/samskivert/jdbc/depot/TableKeyGenerator.java @@ -25,7 +25,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import javax.persistence.TableGenerator; +import com.samskivert.jdbc.depot.annotation.TableGenerator; import com.samskivert.jdbc.JDBCUtil; diff --git a/src/java/com/samskivert/jdbc/depot/annotation/Column.java b/src/java/com/samskivert/jdbc/depot/annotation/Column.java new file mode 100644 index 00000000..b33ba72c --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/Column.java @@ -0,0 +1,65 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Is used to specify a mapped column for a persistent property or field. If no Column annotation + * is specified, the default values are applied. + */ +@Target(value=ElementType.FIELD) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface Column +{ + /** + * The name of the column. Defaults to the field name. + */ + String name () default ""; + + /** + * Whether the property is a unique key. This is a shortcut for the UniqueConstraint annotation + * at the table level and is useful for when the unique key constraint is only a single + * field. This constraint applies in addition to any constraint entailed by primary key mapping + * and to constraints specified at the table level. + */ + boolean unique () default false; + + /** + * Whether the database column is nullable. Note: this default differs from the value + * used by the EJB3 persistence framework. + */ + boolean nullable () default false; + + /** + * The SQL fragment that is used when generating the DDL for the column. Defaults to the + * generated SQL to create a column of the inferred type. + */ + String columnDefinition () default ""; + + /** + * The column length. (Applies to String and byte[] columns.) + */ + int length () default 255; +} diff --git a/src/java/com/samskivert/jdbc/depot/Computed.java b/src/java/com/samskivert/jdbc/depot/annotation/Computed.java similarity index 74% rename from src/java/com/samskivert/jdbc/depot/Computed.java rename to src/java/com/samskivert/jdbc/depot/annotation/Computed.java index dfa51eeb..7580a5dd 100644 --- a/src/java/com/samskivert/jdbc/depot/Computed.java +++ b/src/java/com/samskivert/jdbc/depot/annotation/Computed.java @@ -3,7 +3,7 @@ // // samskivert library - useful routines for java programs // Copyright (C) 2006 Michael Bayne -// +// // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation; either version 2.1 of the License, or @@ -18,24 +18,26 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -package com.samskivert.jdbc.depot; +package com.samskivert.jdbc.depot.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import com.samskivert.jdbc.depot.Query; + /** - * Marks a field as computed, meaning it's ignored for schema purposes, it does not directly - * correspond to a column in a table, and thus its value must be overridden in the {@link Query}. + * Marks a field as computed, meaning it is ignored for schema purposes and it does not directly + * correspond to a column in a table, thus its value must be overridden in the {@link Query}. */ @Retention(value=RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.TYPE }) public @interface Computed { - /** If this value is false, the field is not populated by Depot at all. */ - boolean required() default true; - + /** If this value is false, the field is not populated at all. */ + boolean required () default true; + /** A non-empty value here is taken as literal SQL and used to populate the computed field. */ - String fieldDefinition() default ""; + String fieldDefinition () default ""; } diff --git a/src/java/com/samskivert/jdbc/depot/annotation/Entity.java b/src/java/com/samskivert/jdbc/depot/annotation/Entity.java new file mode 100644 index 00000000..93d67589 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/Entity.java @@ -0,0 +1,37 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Specifies the primary field(s) of an entity. + */ +@Target(value=ElementType.TYPE) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface Entity +{ + /** The name of an entity. Defaults to the unqualified name of the entity class. */ + String name () default ""; +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/GeneratedValue.java b/src/java/com/samskivert/jdbc/depot/annotation/GeneratedValue.java new file mode 100644 index 00000000..bae7c06a --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/GeneratedValue.java @@ -0,0 +1,43 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Provides for the specification of generation strategies for the values of primary keys. The + * GeneratedValue annotation may be applied to a primary field in conjunction with the {@link Id} + * annotation. + */ +@Target(value=ElementType.FIELD) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface GeneratedValue +{ + /** Identifies which generator should be used to generate this value when using a table or + * sequence generator. */ + String generator () default ""; + + /** Identifies the strategy to be used to generate this value. */ + GenerationType strategy () default GenerationType.AUTO; +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/GenerationType.java b/src/java/com/samskivert/jdbc/depot/annotation/GenerationType.java new file mode 100644 index 00000000..2e98aade --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/GenerationType.java @@ -0,0 +1,51 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +/** + * Defines the types of primary key generation. + */ +public enum GenerationType +{ + /** + * Indicates that the persistence provider must assign primary keys for the entity using an + * underlying database table to ensure uniqueness. + */ + TABLE, + + /** + * Indicates that the persistence provider must assign primary keys for the entity using + * database sequences. + */ + SEQUENCE, + + /** + * Indicates that the persistence provider must assign primary keys for the entity using + * database identity column. + */ + IDENTITY, + + /** + * Indicates that the persistence provider should pick an appropriate strategy for the + * particular database. + */ + AUTO; +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/Id.java b/src/java/com/samskivert/jdbc/depot/annotation/Id.java new file mode 100644 index 00000000..0e9bab38 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/Id.java @@ -0,0 +1,35 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Specifies the primary key property or field of an entity. + */ +@Target(value=ElementType.FIELD) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface Id +{ +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/Table.java b/src/java/com/samskivert/jdbc/depot/annotation/Table.java new file mode 100644 index 00000000..47702293 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/Table.java @@ -0,0 +1,43 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation specifies the primary table for the annotated entity. If no Table + * annotation is specified for an entity class, the default values apply. + */ +@Target(value=ElementType.TYPE) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface Table +{ + /** + * Unique constraints that are to be placed on the table. + * These constraints apply in addition to any constraints specified by the Column + * annotation and constraints entailed by primary key mappings. + * Defaults to no additional constraints. + */ + public UniqueConstraint[] uniqueConstraints () default {}; +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/TableGenerator.java b/src/java/com/samskivert/jdbc/depot/annotation/TableGenerator.java new file mode 100644 index 00000000..f17f2644 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/TableGenerator.java @@ -0,0 +1,80 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation defines a primary key generator that may be referenced by name when a generator + * element is specified for the GeneratedValue annotation. A table generator may be specified on + * the entity class or on the primary key field. The scope of the generator name is global to the + * persistence unit (across all generator types). + */ +@Target(value={ElementType.TYPE, ElementType.FIELD}) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface TableGenerator +{ + /** + * A unique generator name that can be referenced by one or more classes to be the + * generator for id values. + */ + String name (); + + /** + * Name of table that stores the generated id values. Defaults to a name chosen by persistence + * provider. + */ + String table () default ""; + + /** + * Name of the primary key column in the table Defaults to a provider-chosen name. + */ + String pkColumnName () default ""; + + /** + * Name of the column that stores the last value generated Defaults to a provider-chosen name. + */ + String valueColumnName () default ""; + + /** + * The primary key value in the generator table that distinguishes this set of generated values + * from others that may be stored in the table Defaults to a provider-chosen value to store in + * the primary key column of the generator table + */ + String pkColumnValue () default ""; + + /** + * The initial value to be used when allocating id numbers from the generator. The default + * initial value is 1. Note: this default differs from the value used by the EJB3 + * persistence framework. + */ + int initialValue () default 1; + + /** + * The amount to increment by when allocating id numbers from the generator. The default + * allocation size is 1. Note: this default differs from the value used by the EJB3 + * persistence framework. + */ + int allocationSize () default 1; +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/Transient.java b/src/java/com/samskivert/jdbc/depot/annotation/Transient.java new file mode 100644 index 00000000..e90d53de --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/Transient.java @@ -0,0 +1,36 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation specifies that the property or field is not persistent. It is used to annotate + * a property or field of an entity class, mapped superclass, or embeddable class. + */ +@Target(value=ElementType.FIELD) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface Transient +{ +} diff --git a/src/java/com/samskivert/jdbc/depot/annotation/UniqueConstraint.java b/src/java/com/samskivert/jdbc/depot/annotation/UniqueConstraint.java new file mode 100644 index 00000000..f114ffd5 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/annotation/UniqueConstraint.java @@ -0,0 +1,39 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used to specify that a unique constraint is to be included in the + * generated DDL for a table. + */ +@Target(value={}) +@Retention(value=RetentionPolicy.RUNTIME) +public @interface UniqueConstraint +{ + /** + * An array of the column names that make up the constraint + */ + public String[] columnNames () default {}; +} diff --git a/tests/src/java/com/samskivert/jdbc/depot/TestRecord.java b/tests/src/java/com/samskivert/jdbc/depot/TestRecord.java index 1806a527..8ff0145b 100644 --- a/tests/src/java/com/samskivert/jdbc/depot/TestRecord.java +++ b/tests/src/java/com/samskivert/jdbc/depot/TestRecord.java @@ -6,8 +6,8 @@ package com.samskivert.jdbc.depot; import java.sql.Date; import java.sql.Timestamp; -import javax.persistence.Id; -import javax.persistence.Column; +import com.samskivert.jdbc.depot.annotation.Id; +import com.samskivert.jdbc.depot.annotation.Column; import com.samskivert.util.StringUtil;