I still haven't seen a Jira announcement on this and it's a fairly
serious issue:
On Sat, 2008-01-19 at 05:51 -0500, Tim Holloway wrote:
> Affected openjpa release: 1.1.0 SVN source pull & build 2008-01-17
> Affected product/subsystem: ReverseMappingTool
>
> Severity: SEVERE Invalid source code is generated
>
> Synopsis: Invalid code is generated when internally-generated foreign
> key names are used and the ReverseMappingTool is run with the "-fk"
> option set to true.
>
> Description: If a constraint name is not explicitly defined, PostgreSQL
> v7 synthesizes constraint names in the form "$n", where n is numeric,
> viz:
>
> Table "public.bus_stops"
> Column | Type | Modifiers
> -------------------+-----------------------+------------------------
> stop_id | integer | not null
> street1_name | character varying(32) | not null
> street1_qualifier | character varying(11) |
> street1_block | integer |
> street2_name | character varying(32) |
> street2_qualifier | character varying(11) |
> street2_block | integer |
> longitude | numeric(16,8) | not null default 0.0
> latitude | numeric(16,8) | not null default 0.0
> elevation | numeric(16,8) | not null default 0.0
> description | text |
> sheltered | boolean | not null default false
> inbound | boolean | not null
> street1_type | character varying(8) | not null
> street2_type | character varying(8) | not null
> street1_direction | character varying(5) | not null
> street2_direction | character varying(5) | not null
> Indexes:
> "stop2_pkey" primary key, btree (stop_id)
> Foreign-key constraints:
> "$1" FOREIGN KEY (street1_direction) REFERENCES
> lk_street_directions(street_direction_id)
> "$2" FOREIGN KEY (street2_direction) REFERENCES
> lk_street_directions(street_direction_id)
> "fk_bus_stops_street1_type" FOREIGN KEY (street1_type) REFERENCES
> lk_street_types(street_type_id)
> "fk_bus_stops_street2_type" FOREIGN KEY (street2_type) REFERENCES
> lk_street_types(street_type_id)
>
> Consequences: Note the generated code for synthesized and explicit
> constraint names:
> /**
> * Auto-generated by:
> * org.apache.openjpa.jdbc.meta.ReverseMappingTool
> $AnnotatedCodeGenerator
> */
> @Entity
> @Table(schema="public", name="bus_stops")
> public class BusStops {
> @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
> @JoinColumn(name="street1_direction", nullable=false)
> private LkStreetDirections 1; //<==== WRONG
>
> @OneToMany(targetEntity=com.mousetech.jta.persist.TripStops.class,
> mappedBy="1", cascade=CascadeType.MERGE)
> //VVVVV WRONG VVVV
> private Set<TripStops> 1Inverses = new HashSet<TripStops>();
>
> @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
> @JoinColumn(name="street2_direction", nullable=false)
> private LkStreetDirections 2; //<==== WRONG
>
> @OneToMany(targetEntity=com.mousetech.jta.persist.RouteStops.class,
> mappedBy="2", cascade=CascadeType.MERGE)
> //VVVVV WRONG VVVV
> private Set<RouteStops> 22Inverses = new HashSet<RouteStops>();
>
> @Basic
> @Column(columnDefinition="text", length=2147483647)
> private String description;
>
> @Basic
> private double elevation;
>
> @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
> @JoinColumn(name="street1_type", nullable=false)
> private LkStreetTypes fkBusStopsStreet1Type; //<==== CORRECT
>
> @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
> @JoinColumn(name="street2_type", nullable=false)
> private LkStreetTypes fkBusStopsStreet2Type; //<==== CORRECT
>
> (remainder of code omitted as immaterial).
>
>
Probable cause: Mis-interpretation of code templating mechanism of "$n"
as a named variable instead of as a literal value. Also, of course, $n
isn't suitable for use as a Java name.
Workarounds: Requires modification of the PostgreSQL schema to replace
the auto-generated constrain names with explicitly-defined names.