openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yves PIEL <ypiel.talend....@gmail.com>
Subject Re: Escape default value on CREATE TABLE
Date Wed, 13 Jun 2018 10:51:39 GMT
Hello Matthew,

Thanks for your answer.

My use case is really to generate SQL queries programmatically, as my
example shows, not from java entities.

Regards,
Yves

On 13 June 2018 at 12:18, Matthew Broadhead <matthew.broadhead@nbmlaw.co.uk>
wrote:

> i don't know your use case but i generate the tables automatically from
> the java entities by setting the SynchroniseMappings property in
> persistence.xml, e.g.
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="2.1"
>     xmlns="http://xmlns.jcp.org/xml/ns/persistence"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
> http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
>     <persistence-unit name="myDb">
>         <jta-data-source>myDb</jta-data-source>
>         <class>com.example.entities.jpa.Entity1</class>
>         <class>com.example.entities.jpa.Entity2</class>
>         <properties>
>             <property name="openjpa.jdbc.SynchronizeMappings"
>                 value="buildSchema(ForeignKeys=true)" />
>         </properties>
>     </persistence-unit>
> </persistence>
>
> an example entity might be:
> package com.example.entities.jpa;
>
> import javax.persistence.Entity;
> import javax.persistence.Id;
> import javax.persistence.Version;
>
> @Entity
> public class Entity1 implements Serializable {
>     private static final long serialVersionUID = 1L;
>
>     @Id
>     private Long id;
>
>     @Version
>     private Long version;
>
>     @Column(length = 255)
>     private String name;
>
>     ...getters setters
> }
>
> and you could just set the default when you initialise the Entity? like
> Entity1 entity1 = new Entity1();
> entity1.setName("O,K");
>
>
> On 12/06/18 15:21, Yves PIEL wrote:
>
>> Hi,
>>
>> I try to use JPA to generate CREATE TABLE statements.All works fine but
>> default values of VARCHAR.
>>
>> For example, I add a VARCHAR column with default value with :
>>
>> DBIdentifier name = DBIdentifier.newColumn("na-me");
>> Column cName = table.addColumn(name);
>> cName.setType(Types.VARCHAR);
>> cName.setNotNull(true);
>> cName.setSize(255);
>> cName.setDefault("O,K");
>>
>> And the generated code is:
>>
>> DB => CREATE TABLE "My-schema".MaTable (id1 INTEGER NOT NULL, id2
>> VARCHAR(500) NOT NULL, "na-me" VARCHAR(255) DEFAULT O,K NOT NULL, âge
>> TINYINT DEFAULT 20, salary DECIMAL, CONSTRAINT idkeys PRIMARY KEY (id1,
>> id2))
>>
>>
>> 'O,K' will corrupt my SQL query. How can I force the escape of default
>> values ?
>>
>> Regards,
>> Yves
>>
>>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message