Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support SQL arrays #653

Open
gavinking opened this issue Sep 20, 2024 · 8 comments
Open

support SQL arrays #653

gavinking opened this issue Sep 20, 2024 · 8 comments
Labels
candidate-for-4 Good candidate for JPA 4

Comments

@gavinking
Copy link
Contributor

gavinking commented Sep 20, 2024

Both Hibernate and EclipseLink have an @Array annotation for mapping Java arrays to SQL array types.

I therefore think we should add the following annotation to JPA:

@Target({FIELD, METHOD})
@Retention( RUNTIME )
public @interface Array {
    /**
     * The maximum length of the array.
     */
    int length();
}

You could use it like this:

@Array(length=10)
String[] tags;
@gavinking gavinking added the candidate-for-4 Good candidate for JPA 4 label Sep 20, 2024
@jakartaee jakartaee deleted a comment Sep 20, 2024
@beikov
Copy link

beikov commented Sep 20, 2024

IMO we should leave fetch and optional to @Basic. Also, the length should maybe be optional with a reasonable default that allows vendor flexibility i.e. value 0.

@gavinking
Copy link
Contributor Author

IMO we should leave fetch and optional to @Basic.

Ah yes, you're right. For some reason I thought that @Lob and @Enumerated worked this way. But I misremembered. We should be consistent with those annotations, and let this combine with @Basic.

Also, the length should maybe be optional with a reasonable default that allows vendor flexibility

That sounds like a recipe for unnecessary lack of portability to me.

@gavinking
Copy link
Contributor Author

gavinking commented Sep 20, 2024

That sounds like a recipe for unnecessary lack of portability to me.

I mean, unless you want to actually specify what the default is. But I have no intuition as to what would be a sensible default here.

@beikov
Copy link

beikov commented Sep 20, 2024

The default could be the maximum possible size a DB allows. Since Oracle requires to specify array sizes, but other databases support just "unlimited" (within the bounds of row format limits) elements, a default to max might be nice. If Oracle users care about row size and perf (not sure if the max size affects that), they can specify a lower value.

@gavinking
Copy link
Contributor Author

The default could be the maximum possible size a DB allows.

Wouldn't that be extremely inefficient for small arrays?

@beikov
Copy link

beikov commented Sep 20, 2024

Like I wrote, every DB other than Oracle supports unlimited size by default or doesn't even allow specifying a size limit in the column definition. To restrict the size, we'd have to emit a check constraint.
I don't know if it is inefficient on Oracle to ask for an array of e.g. 10000 when only storing 1 element, but like I wrote, if users target Oracle, they will want to define that length anyway.

@lukasj
Copy link
Contributor

lukasj commented Sep 20, 2024

If I use this on a raw Collection, I need something like targetEntity, like in ie @OneToMany(targetEntity=...)

@beikov
Copy link

beikov commented Sep 20, 2024

Yeah, Class<?> targetClass() default void.class; is probably appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
candidate-for-4 Good candidate for JPA 4
Projects
None yet
Development

No branches or pull requests

3 participants