Ordering in many-to-many relation (Java Spring) - java

There is 2 models with relation many-to-many:
#Entity
public class Map {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JsonIgnore
private long mapId;
#NotBlank
private String title;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "route_points",
joinColumns = #JoinColumn(name = "mapId", referencedColumnName = "mapId"),
inverseJoinColumns = #JoinColumn(name = "pointId", referencedColumnName = "pointId"))
private Set<Point> points;
}
#Entity
public class Point {
#Id
private String pointId;
#NotBlank
private String city;
#ManyToMany(mappedBy = "points")
private Set<Map> maps;
}
I have to save an order of points in the set and record it to the intermediate table. How it can be done? Thx.

Use a List instead of a Set in combination with the #OrderColumn annotation:
https://docs.oracle.com/javaee/6/api/javax/persistence/OrderColumn.html
This will create an additional column in route_points to track the order of elements, including insertion, deletion, and reordering.
Please note that JPA will maintain the ordering as consecutive numbers, so most (if not all) structural modifications to the list will result in an update statement for each and every element.

Set basicly does not hold order of insertion. LinkedHashSet however will keep order of insertion of elements you could try that. It will store Point into database in proper order, but most probably it again will be mixed up after fetching from the database. You have to viable options here:
Dont use Set use List insteed.
Stick with Set and add private Integer index field to Pointand store proper indexes - you will be able to sort that after fetch without any problems
If you ensure that points will be persisted in right order into databse, then you could ommit additional column and sort by id assuming you are using unique, no gaps, autogenerated sequence for PKs

Related

Backward compatibility of #orderColumn: hibernate

I have 2 entities with oneToMany relationship. I want to maintain the insertion order for child entity. I used #orderColumn for that. Code:
Parent Class:
#Entity
public class Order{
private String orderId;
#OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
#NotEmpty
#OrderColumn
private List<OrderItem> orderItems = new ArrayList<>();
}
Child class:
#Entity
public class OrderItem{
#Id
private String orderItemId;
#ManyToOne
#JoinColumn(name = "order_id", nullable = false)
private Order order;
}
The issue that I'm facing here is orderColumn is not backward compatible. i.e. it adds an column in the child table with name "order_item_order". It works fine for the records that are getting created after this change but for the previous records, the column is null and it results in below exception:
org.hibernate.HibernateException: null index column for collection
I have tried setting the default value to 0 for the column. In that case it returns only one record for child.
Suggestions please.
You have two solutions :
Proceed with the #OrderColumn but fill it with the right values : index starting at 0, incrementing by 1 (migrate your data thanks to a sql scripts or a two steps migration from java)
Proceed with #OrderBy annotation : add a creation_date column, fill it when you store the object (like in the create(ModelClass model) method of your repository) and set it to a default value in the past

How to represent a Map<EntityType, Integer> relation using JPA?

I have a Order entity, and a Product entity. An order may have a number of pairs, representing the product and the number sold. What is an approprate relation in JPA to represent it?
(So far I have only found methods to associate a collection of EntityA with EntityB. e.g. EntityA contains a List<EntityB>. )
If the quantity is all there is to this association and you do not need to navigate from Product→Order, you can consider the Integer quantity as an element collection and do the following - Product stays the same:
public class Order {
#ElementCollection // 1
#CollectionTable(name="ORDER_PRODUCT_QTY") // 2
#MapKeyJoinColumn(name="PRODUCT_ID") // 3
#Column(name="QUANTITY") // 4
private Map<Product, Integer> quantities;
}
It is a collection of basic types (integers for the quantity), keyed by the entity
It is multivalued, so needs a separate table; you optionally want to specify its name
The separate collection table will contain column(s) pointing to the Order entity, column(s) pointing to the Product and a column for the quantity value itself. This lets you set the name of the FK referencing the Product table and is optional.
This lets you specify the name of the column holding the quantity value. Optional too.
If you have reasons to believe that this is not enough then you may want to create a distinct entity representing the association, like:
Order ← OrderItem → Product
Order has many OrderItems, Product has many OrderItems, Order has many Products indirectly through OrderItem, Product can be found in many Orders, indirectly through OrderItem and the quantity is in the OrderItem. Representing this kind of "relation with value" as an entity is more flexible than collection mapping.
you have to map entity like
In Order.jave
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "order_Id")
#JsonBackReference
private List<Product> product = new ArrayList<Product>();
In Product.jave
#ManyToOne
#JoinColumn(name = "product_Id", referencedColumnName = "product_Id", nullable = false, insertable = false, updatable = false)
#JsonBackReference
private Order order;
I am also using the above code but it is not working for me.
Below is my code.
#ElementCollection(fetch = FetchType.EAGER)
#CollectionTable(name="content_package_component_level_languages_language_assessment_map", joinColumns=#JoinColumn(name="id"))
#MapKeyJoinColumn(name="language_assessment_map_key", referencedColumnName = "id")
#Column(name="language_assessment_map")
private Map<Lang, Integer> languageAssessmentMap;

Duplicate entries in hibernate query result

Model O has an element collection of an enum type.
The abbreviated version
#Entity
class O {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ElementCollection(fetch=FetchType.EAGER)
#JoinTable(name = "o_s", joinColumns = { #JoinColumn(name = "o_id") })
#Column
private Set<SomeEnum> ss;
}
I am querying for all instances of O as follows
List<O> ret=session.createCriteria(O.class).list();
Now the result list contains duplicate entries.
If, there are 3 values in SS field, then the corresponding entry for O will appear 3 times in the result.
If there are 2 values, then the corresponding entry for O will appear 2 times in the result.
However, the database does not contain duplicate entries.
I have verified this behavior empirically.
What am I doing wrong?
That's caused by your eagerly loaded collection. You need to set the DictinctRootEntityResultTransformer to the criteria.
A better alternative, IMO, would be to use HQL:
select distinct o from O o

JPA : How to save list with order in a many-to-many relationship

When I persist a list which has an order, the result in the database has a different order. How can I persist a many-to-many relationship with list order in JPA?
#Entity
#Table(name = "house")
public final class House {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Basic(optional = false)
#Column(name = "name")
private String name
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "house_room",
joinColumns =
#JoinColumn(name = "id_house", referencedColumnName = "id"),
inverseJoinColumns =
#JoinColumn(name = "id_room", referencedColumnName = "id"))
private List<Room> rooms;
}
House object:
Room room1 = new Room();
room1.setName("Kitchen");
Room room2 = new Room();
room2.setName("Bathroom");
Room room3 = new Room();
room3.setName("Bedroom");
List<Run> runs = new ArrayList<Run>();
rooms.add(0,room2);
rooms.add(1,room1);
rooms.add(2,room3);
House cottage = new House();
cottage.setRooms(rooms);
persist(cottage); // order in database should be "room2", "room1" then "room3" but it is saved as "room1", "room2" then "room3"
There are 2 ways to order list in JPA using #OrderBy and #OrderColumn annotations. #OrderColumn is perhaps what you are looking for.
#OrderBy
Specify an ordering rule based on the comparison of a particular attribute of the entity or element
Example:
#OrderBy("name ASC")
private List<Room> rooms;
By adding an #OrderBy annotation on the mapping, we can indicate that we want the rooms to be ordered in ascending alphabetical order by its name attribute.
Note: We needn’t have included the ASC in the #OrderBy annotations because it would be ascending by default.
Simply changing the order of the items in a List in memory will not cause that order to be stored in the database at commit time.
The annotation applies only when collection is retrieved from the database. That means, the order will not be stored in the database but when data is retrieved from the DB and stored to the List collection in memory, they will be ordered.
#OrderColumn
The OrderColumn annotation specifies a column that is used to maintain the persistent order of a list. Example:
#OrderColumn(name="ROOM_ORDER")
private List<Room> rooms;
This means you have an additional column named "ROOM_ORDER" that will indicate the order of your items in your table.
Relational databases do not store order of referenced data. In order to store the order of elements in a list, JPA needs another column in DB table. You may map the column with #OrderColumn in the same ways, as you mapped #JoinColumn (you just need single #OrderColumn, even when you used multiple #JoinColumn annotations)

Hibernate One to Many and Many to One Relation

These two questions answered many of my questions, but I am still struggling to think about in real scenario!
Taking an example from the references.
Assume I have one Order and Multiple Items associated with it.
Now assume One Item can have one Returns but one Returns can have multiple Items.
What I understood is, Order to Items will be One to Many Relation.
Since I need to get Order of an Item, I will create column 'order_fk' in Item table to get it.
//Order entity
#OneToMany
#JoinColumn(name = "order_fk")
private List<Items> items;
//item entity
#Column(name = "order_fk")
private Long orderId;
Return to Items is One to Many mapping. One Return can have multiple Items. But one Item can have only one return id
//Return entity
#OneToMany
#JoinColumn(name = "return_fk")
private List<Items> items;
//item entity
#Column(name = "return_fk")
private Long returnId;
Am I thinking in the right direction? Please make me understand this relations and uni/bi-directional relationships.
Overall, I should get Items for an Order. Get Orderid of given Item. Get Items of Returns and get returnId of given Item.
Reference:
Difference Between One-to-Many, Many-to-One and Many-to-Many?
Hibernate/JPA ManyToOne vs OneToMany
This should be the correct mapping of the entities (database tables and columns are ok)
//Order entity
#OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<Items> items;
//item entity
#ManyToOne
#Column(name = "order_fk")
private Order order;
//Return entity
#OneToMany(mappedBy = "return")
private List<Items> items;
//item entity
#ManyToOne
#Column(name = "return_fk")
private Return return;
cascade = CascadeType.ALL in first mapping means whenever you save/update/delete an order, its items will also be saved/updated/deleted, so adjust it to your needs, on other mapping as well.
Unidirectional relations mean only one side of the relation is aware of the other side. On your examples, if you removed items from Return entity you would have an unidirectional relation between Item and Return. With items present, you have a bidirectional relation.
I think you should use OneToMany in another way:
// Order entity
#OneToMany(mappedBy = "columnInItemsPointingAtOrders")
private List<Items> items;
Please check the docs: http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html.
And one more thing - you are not geting the IDs but entities:
//item entity
#Column(name = "order_fk")
private Order order;
I'm also new to this subject. What helped me to understand the relations is drawing the EER diagram and then synchronizing it to the test DB and experimenting. This does not answer your question but may give a direction.

Categories

Resources