Firstly, I have read Hibernate - One table with multiple entities?.
However, I would like to map two entities to the same table, but I would like both of them to be entities, which I can select from. What I mean:
One table: Person (id, name, dateOfBirth, city, street, zipcode).
Two Entities: Person (id, name, dateOfBirth), Address (id, city,
street, zipcode).
So it's a 1:1 relationship between Entities, but still 1 table in DB.
If I do it using the proposed solution (component keyword) in the above link, I can't query Address directly (I can access it via Person entity). And I want to be able to do
session.createCriteria(Adres.class)
How do I do that?
UPDATE:
I tried the one-to-one association between entities, in Address mapping:
<one-to-one name="Person " class="model_mapowanie_xml.Person "/>
and in Person mapping:
<one-to-one name="Address" class="model_mapowanie_xml.Address "/>
Both classes have fields referring to the other one. Selecting records works fine for that. However, how can I add in one transaction a record using both entities? (Id is db-generated)
Address ad = new Address();
ad.setProperty("Sydney");
Person p = new Person();
p.setProperty("John");
p.setAddress(ad);
session.save(p);
and only Person part is saved, the address property remains empty.
This is very simple to achieve with JPA and Hibernate.
Let's assume you are using the following book database table:
Mapping entities
Now, you can map two entities: Book and BookSummary to this table.
First, we will create a BaseBook abstract class which will be extended by all entities:
#MappedSuperclass
public abstract class BaseBook<T extends BaseBook> {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Column(length = 50)
private String title;
#Column(length = 50)
private String author;
public Long getId() {
return id;
}
public T setId(Long id) {
this.id = id;
return (T) this;
}
public String getIsbn() {
return isbn;
}
public T setIsbn(String isbn) {
this.isbn = isbn;
return (T) this;
}
public String getTitle() {
return title;
}
public T setTitle(String title) {
this.title = title;
return (T) this;
}
public String getAuthor() {
return author;
}
public T setAuthor(String author) {
this.author = author;
return (T) this;
}
}
Now, the BookSummary entity simply extends the BaseBook superclass and adds no additional entity attribute.
#Entity(name = "BookSummary")
#Table(name = "book")
public class BookSummary extends BaseBook<BookSummary> {
}
On the other hand, the Book entity extends the BaseBook superclass and maps the properties attribute.
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(
name = "jsonb",
typeClass = JsonBinaryType.class
)
#DynamicUpdate
public class Book extends BaseBook<Book> {
#Type(type = "jsonb")
#Column(columnDefinition = "jsonb")
private String properties;
public String getProperties() {
return properties;
}
public Book setProperties(String properties) {
this.properties = properties;
return this;
}
public ObjectNode getJsonProperties() {
return (ObjectNode) JacksonUtil
.toJsonNode(properties);
}
}
Persisting entities
This way, you can persist either a Book entity:
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.setTitle("High-Performance Java Persistence")
.setAuthor("Vlad Mihalcea")
.setProperties(
"{" +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99," +
" \"publication_date\": \"2016-20-12\"," +
" \"dimensions\": \"8.5 x 1.1 x 11 inches\"," +
" \"weight\": \"2.5 pounds\"," +
" \"average_review\": \"4.7 out of 5 stars\"," +
" \"url\": \"https://amzn.com/973022823X\"" +
"}"
)
);
or a BookSummary:
entityManager.persist(
new BookSummary()
.setIsbn("978-1934356555")
.setTitle("SQL Antipatterns")
.setAuthor("Bill Karwin")
);
Fetching entities
You can fetch the BookSummary entity:
BookSummary bookSummary = entityManager
.unwrap(Session.class)
.bySimpleNaturalId(BookSummary.class)
.load("978-9730228236");
assertEquals(
"High-Performance Java Persistence",
bookSummary.getTitle()
);
or the Book entity if you want:
Book book = entityManager
.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");
assertEquals(
"High-Performance Java Persistence, 2nd edition",
book.getTitle()
);
Conclusion
So mapping multiple entities to the same database table, not only that it allows us to fetch data more efficiently, but it also speeds up the dirty checking process as Hibernate has to inspect fewer entity properties.
The only drawback of using this approach is that you have to make sure you don’t fetch more than one entity type for the same database table record, as otherwise, this can cause inconsistencies when flushing the Persistence Context.
You should be able to do it using #Table annotation. These entites will be treated as different entites but will be mapped onto same table.
#Entity
#Table(name="PERSON_TABLE")
class Person {}
#Entity
#Table(name"PERSON_TABLE")
class Address {}
Edit:
If you want to save both entities in one transaction you either have to explicitly save them using Session or set cascade property to cascade operations on relationship. I guess you want to cascade operations on Address when you do something on Person. See CascadeType if you use annotations.
In your hbm it would look like
<one-to-one name="Person" class="model_mapowanie_xml.Person" cascade="all"/>
Related
So I have the following code working correctly on my ecommerce site.
#Entity
#Table(name = "v_customer_wishlist")
#NamedQuery(name = "VCustomerWishlist.findAll", query = "SELECT w FROM VCustomerWishlist w")
public class VCustomerWishlist implements Serializable {
#Id
#Column(name = "cart_id")
private int _cartId;
//get/set methods
...
#OneToMany(mappedBy = "_wishlist", cascade = CascadeType.ALL)
private List<VCustomerWishlistItem> _items;
//get/set methods
}
#Entity
#Table(name = "v_customer_wishlist_items")
#NamedQuery(name = "VCustomerWishlistItem.findAll", query = "SELECT i FROM VCustomerWishlistItem i")
public class VCustomerWishlistItem implements Serializable {
...
public VCustomerWishlistItem(int cartId, int productId) {
VCustomerWishlistItemPK id = new VCustomerWishlistItemPK (cartId, productId);
setId(id);
}
#EmbeddedId
private VCustomerWishlistItemPK id; //is PK for cartId and productId
//get/set methods
...
#ManyToOne
#JoinColumn(name = "cart_id")
private VCustomerWishlist _wishlist;
//get/set methods
...
#Column(name = "product_name")
private String _productName;
//get/set methods
...
}
Then somewhere in my backing bean, I could do somethin like this (simplified version):
...
VCustomerWishlist wishlist = getCustomer().getWishlistById(cartId);
...
VCustomerWishlistItem item = new VCustomerWishlistItem(wishlist.getId(), product.getId());
...
item.setSequenceNum(wishlist.getItems().size()+1);
item.setProductName(product.getName());
item.setQuantity(1);
wishlist.addItem(item);
wishlistItemService.save(item);
...
So I can add items (product references) to wishlist and JPA will correctly generate the INSERT INTO queries and so forth.
However, upon thinking about it, I thought it would be better to retrieve this data directly from my 'master_products' table instead of what was stored in the VCustomerWishlistItem.
This way I would always have the most up-to-date productName, unitPrice and so forth for wishlist items saved weeks or months before.
The thing is, if I modify the view in my database to include this additional info by adding joins or subqueries; as soon as add joins or subqueries to my view, it becomes non-inserable/updatable.
I thought that it could be done via JPLQ in one #NamedQuery definition, but I understand these are designed to be used manually when retrieving desired sets. As opposed to the nice built-in way that JPA automatically retrieves the wishlist.items resolving it with the indicating annotation properties.
The fantasy property would be one where I can specify a direct table source for the entity, ignoring the join and subquery tables.
So for example, if the source for 'v_customer_wishlist_items_readonly' was:
SELECT
`cwi`.`cart_id` AS `cart_id`,
`cwi`.`product_id` AS `product_id`,
`cwi`.`sequence_num` AS `sequence_num`,
`mp_readonly`.`product_name` AS `product_name`,
`mp_readonly`.`product_web_id` AS `product_web_id`,
`mp_readonly`.`unit_price` AS `unit_price`,
`cwi`.`quantity` AS `quantity`,
`mp_readonly`.`unit_price`*`csci`.`quantity` AS `item_subtotal`,
`cwi`.`create_datetime` AS `create_datetime`,
`cwi`.`update_datetime` AS `update_datetime`
FROM
`customer_wishlist_items` `cwi` JOIN `master_products` `mp_readonly` ON `cwi`.`product_id` = `mp_readonly`.`product_id`
ORDER BY `cwi`.`sequence_num`;
It would be ideal to have a an annotation where I could indicate that primary table name is 'customer_wishlist_items', so all updates/inserts would only apply to this table and changes to the rest of the read-only fields would be ignored.
So somethint like this:
#Entity
#Table(name = "v_customer_wishlist_items_readonly")
#PrimaryTable(name = "customer_wishlist_items") //fantasy annotation
#NamedQuery(name = "VCustomerWishlistItem.findAll", query = "SELECT s FROM VCustomerWishlistItem s")
public class VCustomerWishlistItem implements Serializable {
...
Does anyone know what would be the correct way of implementing this?
Thanks a lot in advance!
Why not use derived ids or the MapsId to let JPA set your foreign key/id columns for you?
#Entity
#Table(name = "v_customer_wishlist_items")
#NamedQuery(name = "VCustomerWishlistItem.findAll", query = "SELECT i FROM VCustomerWishlistItem i")
public class VCustomerWishlistItem implements Serializable {
...
public VCustomerWishlistItem(VCustomerWishlist cart, Product product) {
this._wishList = cart;
this._product = product;
setId(new VCustomerWishlistItemPK());//JPA will populate this for you
}
#EmbeddedId
private VCustomerWishlistItemPK id; //is PK for cartId and productId
//get/set methods
...
#ManyToOne
#MapsId("cartId")
#JoinColumn(name = "cart_id")
private VCustomerWishlist _wishlist;
//get/set methods
...
#MapsId("productId")
#JoinColumn(name = "product_id")
private Product _product;
//get/set methods
...
}
With this, you don't need to have or lookup the cartId/productId values at all as JPA will figure them out and set them for you, allowing you do just use code like:
VCustomerWishlistItem item = new VCustomerWishlistItem(wishlist, product);
...
item.setSequenceNum(wishlist.getItems().size()+1);
item.setQuantity(1);
wishlist.addItem(item);
wishlistItemService.save(item);
You should probably just set the sequenceNum and add the item to the wishlist in the item constructor, though I'm not a fan this approach to sequencing as it can lead to concurrency issues and problems maintaining it.
You can also do away with the EmbeddedId if you don't need it within your entity and use it as a primary key class; you'd just have to change the property names within it to match the relationships names from the entity:
#Entity
#IdClass(VCustomerWishlistItemPK.class)
#Table(name = "v_customer_wishlist_items")
#NamedQuery(name = "VCustomerWishlistItem.findAll", query = "SELECT i FROM VCustomerWishlistItem i")
public class VCustomerWishlistItem implements Serializable {
...
public VCustomerWishlistItem(VCustomerWishlist cart, Product product) {
this._wishList = cart;
this._product = product;
}
...
#ManyToOne
#Id
#JoinColumn(name = "cart_id")
private VCustomerWishlist _wishlist;
//get/set methods
...
#Id
#JoinColumn(name = "product_id")
private Product _product;
//get/set methods
...
}
The primary key class might then look like:
public class VCustomerWishlistItemPK {
public Integer _product;
public Integer _wishlist;
//optional getter/setter methods..
}
The properties within the ID class must match the names of the properties in your entities, but use the type of the primary key from the referenced class.
I have two entities Person and Movie.
#Entity
public class Person {
..some fields
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "actors")
#OrderBy("id")
private Set<Movie> moviesActor = new TreeSet<>();
}
#Entity
public class Movie {
..fields
#JoinTable(name = "actor_movie",
joinColumns = { #JoinColumn(name = "movie_id") },
inverseJoinColumns = { #JoinColumn(name = "actor_id") })
private Set<Person> actors = new TreeSet<>();
}
There is many to many relationship so there is new table actor_movie to keep it. And how can I get every person that has any movie in its set? So what I want is to achieve is get every person that exists in actor_movie table. I tried used Spring data jpa but couldn't find right query.
Best Practices in entity relations:
Always use fetch = FetchType.LAZY.
When you want to fetch another side of the relation too, use JOIN FETCH Query.This resolves LazyInitializationException of hibernate also.
Always use spring.jpa.open-in-view=false
Example:
By Spring Data JPA with Hibernate as JPA Provider.
Entities:
public class Blog{
...
#ManyToMany(fetch = FetchType.LAZY) //default is LAZY in ManyToMany
#JoinTable(name="blog_tag",
joinColumns = #JoinColumn(name = "blog_id"),
inverseJoinColumns = #JoinColumn(name = "tag_id"))
#OrderBy //order by tag id
private Set<Tag> tags = new HashSet<>();
//2 utility methods in owner side
public void addTag(Tag tag){
tags.add(tag);
tag.getBlogs().add(this);
}
public void removeTag(Tag tag){
tags.remove(tag);
tag.getBlogs().remove(this);
}
//override equals & hashcode
}
public class Tag {
...
#ManyToMany(mappedBy = "tags")
private Set<Blog> blogs = new HashSet<>();
//override equals & hashcode
}
Now suppose, you want to fetch a Blog containing Tag items:
Repository:
#Repository
public interface BlogRepository extends JpaRepository<Blog, Long> {
#Query("select b from Blog b join fetch b.tags where b.name = :name")
Blog getBlog(#Param("name") String blogName);
}
service:
public interface BlogService {
Blog getBlog(String blogName);
}
#Service
public class BlogServiceImpl implements BlogService{
#Autowired
private BlogRepository blogRepository;
#Override
public Blog getBlog(String blogName) {
return blogRepository.getBlog(blogName);
}
}
You only need a single JOIN between Person and Movie. As Hibernate abstracts the existence of the middle table, you don't need to worry about it.
So, with Spring Data Repository:
class PersonRepository extends CrudRepository<Person, Long> {
List<Person> findByMoviesActor();
}
With Jpql:
SELECT person FROM Person person JOIN person.moviesActor movie
Since you are using Fetch type lazy, you need to use join fetch to get moviesActor.
You can use jpql with spring data. I have not tested the queries below, but should work.
public interface PersonRepository extends JpaRepository<Person, Long> { //Long if Person.id is of type Long
#Query("SELECT p FROM Person p LEFT JOIN FETCH p.moviesActor WHERE size(p.moviesActor) > 0");
List<Person> findActors1();
// Or
#Query("SELECT p FROM Person p JOIN FETCH p.moviesActor");
List<Person> findActors2();
}
More about jpql size() operator here: https://www.thoughts-on-java.org/jpql/
You can use join directely :
#Query("SELECT p FROM Person p JOIN p.moviesActor movie");
List findPersonHasMovie();
The Scenario
Class Employee, Class Office, Class OfficeEmployee.
Class office is a Spatial Entity, that can be searched and returns results as expected.
a manyToMany relationship between Office-Employee is mapped with the Class OfficeEmplyee.
Now I need to perform search based on certain people within some range. in other words I have to check for the offices in range and for emplyees who exist at those offices, i.e Searching the OfficeEmployee Entity.
All the three classes are indexed.
OfficeEmployee
// reference Spatial indexed entity Office
#IndexedEmbedded
#ManyToOne (cascade = CascadeType.MERGE)
#JoinColumn(name="office")
private Office office;
// reference to employee
#IndexedEmbedded
#JsonIgnore
#ManyToOne (cascade = CascadeType.MERGE)
#JoinColumn(name="employee")
private Employee employee;
Class Office
#JsonIgnoreProperties(ignoreUnknown=true)
#Spatial(name = "office_location_poi", spatialMode = SpatialMode.HASH )
#Indexed
#Entity
#Embeddable
public class Office implements Serializable,Coordinates {
// some attributes , getters , setters..
#ContainedIn
#OneToMany(mappedBy="office", cascade=CascadeType.ALL)
private List<OfficeEmployee > officeEmployees;
#Latitude
double latitude;
#Longitude
double longitude;
public Coordinates getLocation() {
return new Coordinates() {
#Override
public Double getLatitude() {
return latitude;
}
#Override
public Double getLongitude() {
return longitude;
}
};
}
#Override
public Double getLatitude() {
return latitude;
}
#Override
public Double getLongitude() {
return longitude;
}
}
The Query:
final QueryBuilder builder = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( OfficeEmployee.class ).get();
double centerLatitude = searchTerm.lat;
double centerLongitude =searchTerm.lng;
org.apache.lucene.search.Query luceneQuery = builder.spatial().onField("office").within(searchTerm.distance, Unit.KM)
.ofLatitude(centerLatitude)
.andLongitude(centerLongitude)
.createQuery();
org.hibernate.search.jpa.FullTextQuery hibQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, OfficeEmployee.class);
// sort
Sort distanceSort = new Sort(
new DistanceSortField(centerLatitude, centerLongitude, "office_location_poi"));
hibQuery.setSort(distanceSort);
hibQuery.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS);
hibQuery.setFirstResult(0);
hibQuery.setMaxResults(20);
// results
List<Office>results =hibQuery.getResultList();
The Problem
Now I want to perform my search on the relationship table (OfficeEmployee).
but sounds like I can't get it to work! I checked tutorials, and couldn't find such an example.
Is it possible to use a currently indexed Entity like I have explained?
Do I have to include a #Spatial there in the OfficeEmployee ? but that will require a new indexing separately, I want to use the currently indexed one.
When I run search it says that I need to check #Spatial and #SpatialFieldBridge, and even if I annotate so, the results are empty.
In case that my Spatial entity was implementing the coordinates and doesn't have a separate field for coordinates, where should the #ContainedIn be placed ?
Can anyone please point me in the right direction ?
I have an entity class PositionOrdering which contains an element collection:
#ElementCollection(targetClass = Position.class, fetch = FetchType.EAGER)
#CollectionTable(name = "POSITION_ORDERING_POSITION",
joinColumns = #JoinColumn(name = "position_ordering_id"))
#OrderColumn
List<Position> positions = new ArrayList<>();
When hibernate generates the database structure, it looks like this:
CREATE TABLE wls.position_ordering_position
(
position_ordering_id bigint NOT NULL,
positions_id bigint NOT NULL,
positions_order integer NOT NULL,
...
}
It's ok and exactly what I was expected. But it also generate a unique contsraint on positions_id column. It is strange, because the position id should be unique only per ordering, so any of the following unique keys would be ok:
position_ordering_id + positions_order
position_ordering_id + positions_id
But not on the single column of positions_id.
Because the constraint is generated automatically, I can't ignore or remove it simply.
Can I configure my collection to create correct unique constraint or at least not to create any?
UPDATE:
As for request, here is the skeleton of the Position entity:
#Entity
#SequenceGenerator(name = EntityBase.SEQUENCE_NAME,
sequenceName = "POSITION_ID_SEQ")
#Table(name = "position")
public class Position extends EntityBase {
// Lots of fields, like row, column number, and type, etc.
}
Where EntityBase is a simple class with some utility function and with Id:
#MappedSuperclass
public abstract class EntityBase implements Serializable, Cloneable {
public static final String SEQUENCE_NAME = "SEQUENCE_GENERATOR";
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = SEQUENCE_NAME)
protected Long id;
//..
}
#ElementCollection is used for mapping basic types or #Embedded classes, not entities. From the documentation
An ElementCollection can be used to define a one-to-many relationship to an Embeddable object, or a Basic value (such as a collection of Strings).
Since Position is an #Entity, you should map it as #OneToMany or #ManyToMany. I don't know the exact reason why are you getting that unique key generated, but I guess you can expect unpredictable results if you use the annottion in a was that it was not intended for.
As Predrag Maric described it in the accepted answer, the problem was that Position was not an `Embeddable'. My solution was:
I created a support class which wraps the Position into an #Embeddable entity:
#Embeddable
//#Table(name = "position_ordering_position")
public class PositionOrderingPosition {
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "position_id", nullable = false)
private Position position;
public PositionOrderingPosition() {
}
public PositionOrderingPosition(Position position) {
this.position = position;
}
public Position getPosition() {
return position;
}
}
Also I changed the Element collection to this:
#ElementCollection(fetch = FetchType.EAGER)
#CollectionTable(name = "POSITION_ORDERING_POSITION",
joinColumns = #JoinColumn(name = "position_ordering_id"))
#OrderColumn
List<PositionOrderingPosition> positions = new ArrayList<>();
Now it creates the same table, but with the right constraints.
We are facing problem applying many-to-many relationship using fluent nhibernate automapping.
The simplified form of domain model are as follows:
public class Group
{
private readonly IList<Recipient> _recipients = new List<Recipient>();
public virtual IList<Recipient> Recipients
{
get { return _recipients; }
}
}
public class Recipient
{
private readonly IList<Group> _groups = new List<Group>();
public virtual IList<Group> Groups
{
get { return _ groups; }
}
}
As the code above describes that Group and Recipient are having many-to-many relationship.
We are using automapping feature of fluent nhibernate to map our domain model with database. So, we needed to use Convention for automapping.
Following is code we used for many to many convention:-
public class ManyToManyConvention : IHasManyToManyConvention
{
#region IConvention<IManyToManyCollectionInspector,IManyToManyCollectionInstance> Members
public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance)
{
if (instance.OtherSide == null)
{
instance.Table(
string.Format(
"{0}To{1}",
instance.EntityType.Name + "_Id",
instance.ChildType.Name + "_Id"));
}
else
{
instance.Inverse();
}
instance.Cascade.All();
}
#endregion
}
I found this solution here :
http://blog.vuscode.com/malovicn/archive/2009/11/04/fluent-nhibernate-samples-auto-mapping-part-12.aspx#Many%20to%20Many%20convention
But in above code while debugging both time for Recipients-> Groups and for Groups->Recipients instance.OtherSide is coming not null. The assumption was 1st time instance.OtherSide will be not null and second time it will be null as relationship is applied on one side so we will just apply inverse to that.
So it’s creating 2 mapping tables which are same.
It is load to database to have 2 tables of same schema. Even when I try to save our domain model to database using many to many relationship. It’s saving only 1 side i.e it saves Recipients in the Groups , But not saving Groups in Recipients.In database also it is having entry in only one mapping table not in both.
So , the question is Are we doing the right thing? If not then how to do it.
you could take inverse itself as a criteria
public void Apply(IManyToManyCollectionInstance instance)
{
Debug.Assert(instance.OtherSide != null);
// Hack: the cast is nessesary because the compiler tries to take the Method and not the property
if (((IManyToManyCollectionInspector)instance.OtherSide).Inverse)
{
instance.Table(
string.Format(
"{0}To{1}",
instance.EntityType.Name + "_Id",
instance.ChildType.Name + "_Id"));
}
else
{
instance.Inverse();
}
instance.Cascade.All();
}