Yeah, it does.  I don't know if OpenJPA can execute SQL scripts.  It can
certainly generate them for use with other DB tooling, but I am not aware
of any means for the provider to execute SQL scripts as part of the
EntityManager(Factory) bootstrapping.  OpenJPA does ship with a schema tool
(14.1.6.  Schema Tool Ant Task) but that probably won't help you with an
in-memory Derby DB.

I'll look around and consult with my colleagues if this capability exists
in the form you are looking for, but I do not think it does.

On Fri, Apr 24, 2015 at 11:31 AM, Kay Wrobel <[email protected]> wrote:

> Thanks. I will try that.
>
> I will still be completely stuck populating the database with pre-existing
> "entities", if you know what I mean. I know I could write a class that is
> application-scoped and use the @PostConstruct annotation to do some
> preliminary work, like creating entities using the entity manager. But I
> have like 60-70 records in that database in like 6 or 7 tables and no way
> to turn those database records into Java code that simulated the new
> Entity(x,x,x,x,x); nomenclature. And I really don't want to type all of
> that up. Makes sense?
>
> Kay
>
> > On Apr 24, 2015, at 11:16 AM, Jody Grassel <[email protected]> wrote:
> >
> > @Size is a bean validation annotation, which is not going to govern
> > schema.  Try adding length=1 to your @Column annotation for discountCode
> >
> > @Id
> > @Basic(optional = false)  // not really necessary as @Ids are always
> > non-optional
> > @NotNull
> > @Size(min = 1, max = 1)
> > @Column(name = "DISCOUNT_CODE"*, length=1*)
> > private String discountCode;
> >
> > On Fri, Apr 24, 2015 at 11:08 AM, Kay Wrobel <[email protected]> wrote:
> >
> >> Thank you. Just tried that, but I'm running into errors when it tries to
> >> create a table for an entity that is supposed to become a CHAR(1) field
> >> based on the annotations in the entity, but it tries to generate the
> field
> >> as CHAR(255), which it complains about. Here is a snippet of the entity
> and
> >> the log messages:
> >>
> >>> @Entity
> >>> @Table(name = "DISCOUNT_CODE")
> >>> @XmlRootElement
> >>> @NamedQueries({
> >>>    @NamedQuery(name = "DiscountCode.findAll", query = "SELECT d FROM
> >> DiscountCode d"),
> >>>    @NamedQuery(name = "DiscountCode.findByDiscountCode", query =
> >> "SELECT d FROM DiscountCode d WHERE d.discountCode = :discountCode"),
> >>>    @NamedQuery(name = "DiscountCode.findByRate", query = "SELECT d FROM
> >> DiscountCode d WHERE d.rate = :rate")})
> >>> public class DiscountCode implements Serializable {
> >>>    private static final long serialVersionUID = 1L;
> >>>    @Id
> >>>    @Basic(optional = false)
> >>>    @NotNull
> >>>    @Size(min = 1, max = 1)
> >>>    @Column(name = "DISCOUNT_CODE")
> >>>    private String discountCode;
> >>>    // @Max(value=?)  @Min(value=?)//if you know range of your decimal
> >> fields consider using these annotations to enforce field validation
> >>>    @Column(name = "RATE")
> >>>    private BigDecimal rate;
> >>>    @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode",
> >> fetch = FetchType.EAGER)
> >>>    private Collection<Customer> customerCollection;
> >>
> >>
> >>> SEVERE: EjbTransactionUtil.handleSystemException: The length,
> precision,
> >> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> >> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> >> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000,
> state=42611]
> >>> <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 nonfatal general error>
> >> org.apache.openjpa.persistence.PersistenceException: The length,
> precision,
> >> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> >> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> >> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000,
> state=42611]
> >>>      at
> >> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:559)
> >>>      at
> >> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:455)
> >>>
> >>
> >>
> >> Any idea why that is? I mean there's a @Size annotation in the entity
> that
> >> tell it the max size. Shouldn't the mapper tool take that into account?
> >>
> >> Kay
> >>
> >>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <[email protected]> wrote:
> >>>
> >>> That directive will instruct OpenJPA to introspect the databases to
> >> ensure
> >>> the tables needed for the table schema defined by your ORM exists, and
> >> will
> >>> create the table structures automatically if they do not exist.  Do you
> >>> have a special need that requires OpenJPA to execute a provided SQL
> >> script?
> >>>
> >>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <[email protected]>
> wrote:
> >>>
> >>>> Thanks, Jody. How will that let me provide an SQL script containing
> >> CREATE
> >>>> TABLE and INSERT statements?
> >>>>
> >>>> Kay
> >>>>
> >>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <[email protected]>
> wrote:
> >>>>>
> >>>>> Add the following property to your persistence unit:
> >>>>>
> >>>>> <property name="openjpa.jdbc.SynchronizeMappings"
> >>>>> value="buildSchema(ForeignKeys=true)"/>
> >>>>>
> >>>>>
> >>>>>
> >>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <[email protected]>
> >> wrote:
> >>>>>
> >>>>>> Hi everybody.
> >>>>>>
> >>>>>> I am having a rough time finding a way to initialize an Embedded
> >>>> In-Memory
> >>>>>> Derby database in my web application. I found a reference on
> Oracle's
> >>>> web
> >>>>>> site that states you can initialize a database with DDL and DML
> >>>> statements
> >>>>>> using properties like the following:
> >>>>>>
> >>>>>>>      <properties>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.database.action"
> >>>>>> value="drop-and-create"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-source"
> >>>> value="script"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-script-source"
> >>>>>> value="META-INF/sql/create.sql" />
> >>>>>>>          <property name="javax.persistence.sql-load-script-source"
> >>>>>> value="META-INF/sql/data.sql" />
> >>>>>>>      </properties>
> >>>>>>
> >>>>>>
> >>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
> JEE
> >>>> 6.
> >>>>>> I am working with OpenJPA provided by Apache TomEE, which is
> >>>> openjpa-2.4.0
> >>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
> >>>> release on
> >>>>>> OpenJPA web site is openjpa 2.3.
> >>>>>>
> >>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> >>>> properties?
> >>>>>> I am trying to initialize an in-memory database for a test case I
> try
> >> to
> >>>>>> provide to someone.
> >>>>>>
> >>>>>> Any help would be much appreciated.
> >>>>>>
> >>>>>> Kay Wrobel
> >>>>
> >>>>
> >>
> >>
>
>

Reply via email to