Column ordering SPI and annotations support for static ordering #3989
Replies: 7 comments 40 replies
-
Hi I created the topic on discourse, I would love to participate, but I suspect this may be outside my skill level. My initial idea was a somewhat simple hack where you could set a property that toggles the following feature: If possible would just retain the order in which the fields were defined in the java class. If this is not possible (I suspect the JVM spec does not guarantee any ordering when traversing fields) then add a simple ordering annotation that can be used with a comparator to sort the non-id columns prior to generation. I peeked around in the codebase and it looks doable without too much hassle, but I also understand that you need to keep the codebase clean without too many dirty hacks. Best regards Jens |
Beta Was this translation helpful? Give feedback.
-
Even more important than ordering regular columns would be ordering the columns in the primary key (which is currently not possible). Thus, it would make sense to also address that issue in my opinion - the order of the primary key columns should not necessarily depend on the regular column order. An annotation (or actually two annotations) would be very nice. Maybe on class-level like |
Beta Was this translation helpful? Give feedback.
-
Let's keep in mind that we have pretty much forever not really supported tweaking the generated schema and suggest that users manage their schema themselves if they want specific things. Personally, I've never worked on a real system that would use the schema created by any tool. I'm not particularly against this, but it just seems like unnecessary overhead/complexity.
Those 2 statements are mostly mutually exclusive ;)
But it is - by managing the creation scripts yourself which has generally been the supported approach. |
Beta Was this translation helpful? Give feedback.
-
This part of the proposal I must admit I kinda like.
Grumble another damn property! But yeah, despite my grumble, I ultimately do think this makes sense.
I'm not terribly keen on this part of the proposal. |
Beta Was this translation helpful? Give feedback.
-
So I have not changed my opinion that annotating fields of entities with integers is inelegant, verbose, and extremely fragile (if I add a field, I need to renumber existing fields). The two concrete use cases for this that we know of are:
The first problem is, I believe, much better solved via a plugin SPI which allows for sorting based on column type. As to the second problem, it seems to me that it's much more natural to map the struct type to either:
Fortunately, we've recently added this So you would stick the |
Beta Was this translation helpful? Give feedback.
-
For struct types we drive the order through the constructor now. Record classes have a natural ordering and for regular classes we now have the I propose adding a Other than that, I think there is no need for explicit column ordering. |
Beta Was this translation helpful? Give feedback.
-
So the currently-proposed @Embeddable
@Struct(name="my_point_type")
public static class Point {
private final String y;
private final long z;
private final Integer x;
@Instantiator({"y","z","x"})
public Point(String y, long z, Integer x) {
this.y = y;
this.x = x;
this.z = z;
}
public String getY() {
return y;
}
public Integer getX() {
return x;
}
public long getZ() {
return z;
}
...
} where the names that occur in the list are names of Java fields. That is, it's telling Hibernate which constructor parameters get assigned to which fields by the constructor. It's providing information about the internal impl of the Java class, not about how it relates to the the relational schema. My not-necessarily-100%-rational prejudice here is that this is a rather big break from how all our other annotations work, and wasn't what I had envisaged. I think that our annotations usually work by mapping Java elements to database tables or columns. They don't usually provide information about how the Java class is implemented. So I think this should be changed so that (Unless someone can convince me that that would be worse somehow.) |
Beta Was this translation helpful? Give feedback.
-
Copying from the Trello card (https://trello.com/c/OITwGJ5I/211-column-ordering-spi-and-annotations-support-for-static-ordering)
Developers often use the schemas as generated by Hibernate but currently we order columns in table DDL by name which can lead to inefficient physical layouts on a few DBs due to the need for padding.
I propose we create a SPI for the ordering as well as a configuration parameter and by default
order by coalesce(staticOrder, Integer.MAX_VALUE), max(physicalSizeBytes, 4), physicalSizeBytes > 2048, name
to list small columns first and clobs as well as oversized columns last.This will reduce the necessary padding on DBs like PostgreSQL.
I'd also like to add some kind of possibility to specify the order of columns through a static ordinal per column, if possible, but that would probably require a few changes to some annotations.
Note though, that a static order is required for user defined types as the SQLData interface requires data to be read/written in the order it is defined in the user type.
See https://hibernate.atlassian.net/browse/HHH-15872
Beta Was this translation helpful? Give feedback.
All reactions