openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Miroslav Nachev <m...@space-comm.com>
Subject URGENT: Re: Generic (user) GeneratedValue
Date Tue, 18 Dec 2007 10:16:41 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi,<br>
<br>
>From this mail I understand that the only way in OpenJPA to create
Custom Identifier is using "Sequence" like that:<br>
&nbsp;&nbsp;&nbsp; @Id<br>
&nbsp;&nbsp;&nbsp; @Column(name = "Data_Object_Id", nullable = false)<br>
&nbsp;&nbsp;&nbsp; @SequenceGenerator(name="DataObjectsSeq",
sequenceName="test.DataObjectsGenerator")<br>
&nbsp;&nbsp;&nbsp; @GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="DataObjectsSeq")<br>
&nbsp;&nbsp;&nbsp; private BigDecimal dataObjectId;<br>
<br>
&nbsp;&nbsp;&nbsp; In my test case I am using MySQL which doesn't support Sequences
in
principal and maybe this is a problem. It seams that if the database
doesn't support Sequences it is not possible to create Generic
Generated Value. But this is not the expected behavior because it is
possible to create UUID_STRING and UUID_HEX which are not part of any
databases. When I look at the code I see the following code in class
ImplHelper.java:<br>
&nbsp;&nbsp;&nbsp; private static Object generateValue(StoreContext ctx, ClassMetaData
meta, FieldMetaData fmd, int typeCode)\<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ...<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; switch (strategy)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
case ValueStrategies.SEQUENCE:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; ...<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; return JavaTypes.convert(seq.next(ctx, meta), typeCode);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
case ValueStrategies.UUID_STRING:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; return UUIDGenerator.nextString();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
case ValueStrategies.UUID_HEX:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; return UUIDGenerator.nextHex();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
default:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; return null;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
When I try to create Generic Generated Value using Sequence in database
which doesn't support Sequences I have exception.<br>
Somebody tell me that I have to use last_insert_id as follow:<br>
&nbsp;&nbsp;&nbsp; dbDict.nextSequenceQuery = "SELECT LAST_INSERT_ID()"<br>
I do that but then nobody invoke my class test.DataObjectsGenerator os
some of it methods.<br>
<br>
>From the above code and my tests it seems that theoretically is not
possible to have custom (generic) Generated Value using database which
doesn't support sequences.<br>
Is that a bug? Do you have any idea how to solve it?<br>
<br>
Finally I can replace the database with PostgreSQL but the problem will
not be solved and will still exists.<br>
<br>
<br>
Regards,<br>
Miro.<br>
<br>
<br>
<br>
Patrick Linskey wrote:
<blockquote
 cite="mid:7262f25e0711051332x5e5963a1i49e6043e09c7a59f@mail.gmail.com"
 type="cite">
  <pre wrap="">If you want it to be globally available, you can make it the default
implementation for use by @GenerationType.SEQUENCE by specifying it in
the openjpa.Sequence property.

If you want to use it in just some circumstances, or want to use
different configurations / instances for different types, then you can
set up named sequences as per the examples in the section I pointed
you to earlier, and then list the class name in the value. The example
would look like so:

@SequenceGenerator(name="MyCustomSeq", sequence="fully.qualified.ClassName")

Or, if you wanted to set some configuration options on the instance:

@SequenceGenerator(name="MyCustomSeq",
sequence="fully.qualified.ClassName(InitialValue=5,BitLength=32")

This would work provided that your class had methods called
setInitialValue() and setBitLength(), or public fields with those
names, or implemented the GenericConfigurable interface.

Similarly, the syntax for specifying a sequence globally via the
openjpa.Sequence setting would look like so:

&lt;property name="openjpa.Sequence" value="fully.qualified.ClassName"/&gt;

OpenJPA configuration settings that can implement a particular
interface use this format plus more-friendly aliases quite frequently.

-Patrick

On Nov 5, 2007 1:21 PM, Miroslav Nachev <a class="moz-txt-link-rfc2396E" href="mailto:miro@space-comm.com">&lt;miro@space-comm.com&gt;</a>
wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">The implementation of Seq interface is the easiest part of the task.
I can
not see how to configure JPA to use my Seq implementation instead of the
standard. Can I do that with annotation or I have to use another tricks?


Miro.

On 11/5/07, Patrick Linskey <a class="moz-txt-link-rfc2396E" href="mailto:plinskey@gmail.com">&lt;plinskey@gmail.com&gt;</a>
wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">Gotcha. To do that, you'll need to implement the
org.apache.openjpa.kernel.Seq interface. Take a look at the docs [1]
for details. That section of the docs also has some examples that
should help you use your custom sequence.

[1]
<a class="moz-txt-link-freetext" href="http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_sequence">http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_sequence</a>

-Patrick

On Nov 5, 2007 1:01 PM, Miroslav Nachev <a class="moz-txt-link-rfc2396E" href="mailto:miro@space-comm.com">&lt;miro@space-comm.com&gt;</a>
wrote:
      </pre>
      <blockquote type="cite">
        <pre wrap="">No. I want to have my own class with which to generate the Ids.
For example I have binary(32) field which is the Primary Key. I would
        </pre>
      </blockquote>
      <pre wrap="">like
      </pre>
      <blockquote type="cite">
        <pre wrap="">to generate the values for this PK in my own way.


Miro.


On 11/5/07, Patrick Linskey <a class="moz-txt-link-rfc2396E" href="mailto:plinskey@gmail.com">&lt;plinskey@gmail.com&gt;</a>
wrote:
        </pre>
        <blockquote type="cite">
          <pre wrap="">Hi,

Can you describe what you mean by a "generic" GeneratedValue? Do you
mean that you'd like to use the @GeneratedValue annotation on
non-identity fields? If so, then that is possible in OpenJPA.

-Patrick

On Nov 5, 2007 12:08 PM, Miroslav Nachev <a class="moz-txt-link-rfc2396E" href="mailto:miro@space-comm.com">&lt;miro@space-comm.com&gt;</a>
wrote:
          </pre>
          <blockquote type="cite">
            <pre wrap="">Hi,

Is there any way to have Generic GeneratedValue in OpenJPA. I know
            </pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">that
      </pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre wrap="">this
          </pre>
          <blockquote type="cite">
            <pre wrap="">is possible in Hibernate JPA but I would like to use OpenJPA
if is
            </pre>
          </blockquote>
          <pre wrap="">possible.
          </pre>
          <blockquote type="cite">
            <pre wrap="">Any suggestions?


Regards,
Miro.

            </pre>
          </blockquote>
          <pre wrap="">

--
Patrick Linskey
202 669 5907

          </pre>
        </blockquote>
      </blockquote>
      <pre wrap="">

--
Patrick Linskey
202 669 5907

      </pre>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->


  </pre>
</blockquote>
<br>
</body>
</html>

Mime
View raw message