JPA OneToMany-Mapping without mapping class for Many-side - java

In a Spring Boot application using JPA 2.1 and Hibernate, there are two (PostgreSQL) tables of interest:
entity external_id
-- --
id serial id serial
... entity_id int
... external_id int
The relation between entity and external_ids is obviously OneToMany, which I want to use in the JPA mapping as well. A simple way to do this is to create #Entity-mappings for each table and use a #OneToMany-relation:
#Entity
public class Entity {
#Id
private Integer id;
#OneToMany(mappedBy= "entityId")
private Set<ExternalId> externalIds;
}
#Entity
public class ExternalId {
#Id
private Integer id;
#ManyToOne
private Integer entityId;
private Integer externalId;
}
But since the table external_ids just holds a list of numbers for each member of entity, I would like to go without an explicit mapping of the table external_id and immediately map the values of external_id.external_id:
#Entity
public class Entity {
#Id
private Integer id;
#OneToMany(???)
private Set<Integer> externalIds;
}
Is this possible with JPA 2.1 and if so how?

You can utilize #ElementCollection for this purpose:
#ElementCollection
#CollectionTable(name = "TableName", joinColumns=#JoinColumn(name = "JoinColumnName"))
private Set<Integer> externalIds;

Related

hibernate #OneToMany join table not being created

I have two tables Quiz and Question which I want to associate with a #OneToMany relationship but the join table is not being created in mysql workbench database. Here are the entities :
Quiz.java
#Entity
public class Quiz {
// attributes :
private Integer idQuiz;
private String quizTopic;
#OneToMany
#JoinColumn(name = "quiz_Id")
private List<Question> questions;
// constructors
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "quiz_Id")
public Integer getIdQuiz() {
return idQuiz;
}
#Column(name = "quiz_topic")
public String getQuizTopic() {
return quizTopic;
}
//setters
}
Question.java
#Entity
public class Question {
// attributes
private Integer idQuestion;
private String value;
private String op1;
private String op2;
private String op3;
private String correctAnswer;
// constructors
// Getters :
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "question_Id")
public Integer getIdQuestion() {
return idQuestion;
}
#Column(name = "question_value")
public String getValue() {
return value;
}
#Column(name = "question_op1")
public String getOp1() {
return op1;
}
#Column(name = "question_op2")
public String getOp2() {
return op2;
}
#Column(name = "question_op3")
public String getOp3() {
return op3;
}
#Column(name = "question_op4")
public String getCorrectAnswer() {
return correctAnswer;
}
//setters
}
no foreign keys are created
here is what I get after running
Hibernate: create table question (question_id integer not null auto_increment, question_op4 varchar(255), question_op1 varchar(255), question_op2 varchar(255), question_op3 varchar(255), question_value varchar(255), primary key (question_id)) engine=InnoDB
Hibernate: create table quiz (quiz_id integer not null auto_increment, quiz_topic varchar(255), primary key (quiz_id)) engine=InnoDB
application.properties:
spring.main.web-application-type=none
spring.application.ui.title=Quiz
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/quiz?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=justpass
spring.jpa.show-sql=true
The annotation
#OneToMany
private ArrayList<Question> questions;
will not create a join table. It should create a foreign-key in the Question table to the Quiz table. This foreign key is not the same as the
private Integer idQuestion;
column.
For your solution it dependes wether you want to build a uni- or bidirectional #OneToMany relationship. If you want a unidirectional one-to-many relationship you have to define a #JoinColumn to tell Hibernate that it shall create a foreign key with the given name in the related table. Most of the time a unidirectional one to many relationship is the easiest and sufficient way to go.
Unidirectional
#OneToMany
#JoinColumn(name = "quiz_id")
private List<Question> questions;
By this Hibernate will create the foreign key with name "quiz_id" in the question table.
In the bidirectional case we are able to access the quiz from the question vice versa the questions from the quiz. In this case you will you will have to define the variable which shall represent the parent class in the child class. For example if the quiz shall be the parent class, you will define the annotation. #OneToMany(mappedBy = "quiz"). Additionally to this you will have to define the question to be the #ManyToOne side as well. All in all:
Bidirectional
Quiz.java:
#OneToMany(mappedBy = "quiz")
private List<Question> questions;
Question.java
#ManyToOne(fetch = FetchType.LAZY)
private Quiz quiz;
You will want to define the fetch type to be lazy due to performance reasons (I even ran into some overflow errors in the past, without the lazy method).
Keep in mind that if you are using the bidirectional mapping together with serialization libraries such as jackson, you will run into the JSON infinite recursion problem when de-/serializing from/to JSON. In this case you will want to use #JsonIdentityInfo to serialize the id only instead of the complete entity (which will lead to infinite recursion) or #JsonIgnore respectively #JsonManagedReference and #JsonBackReference to not serialize the child dependencies of an entity.
I also recommend to use the properties cascade defining the cascade type and orphanRemoval for the deletion of entities when you work with relationships, but I did not want to blow up my answer with unrelated information.
It looks suspicious that you try to mix up access strategies in the Quiz entity.
As it is stated in the documentation:
As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the #Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. When placed on the identifier getter, Hibernate will use property-based access.
So, instead of this:
#OneToMany
#JoinColumn(name = "quiz_Id")
private List<Question> questions;
put the #OneToMany and #JoinColumn(name = "quiz_Id") annotations on the appropriate getter:
#OneToMany
#JoinColumn(name = "quiz_Id")
public List<Question> getQuestions() {
return questions;
}
then try to recreate your schema by setting the spring.jpa.hibernate.ddl-auto property like this:
spring.jpa.hibernate.ddl-auto=create

How to stop Hibernate from eagerly fetching a relationship when it is mapped using a column (referencedColumnName) different than the primary key?

I'm mapping a relationship that does not use the entity's primary key. Using "referencedColumnName" with a column different than the primary key causes hibernate to eagerly fetch the association, by issuing an extra select, even when it's tagged with FetchType.LAZY.
My goal is to make it behave like a regular mapping, meaning it wouldn't issue an extra query every time I need to query the main entity.
I have already tried using #LazyToOne(LazyToOneOption.NO_PROXY), which sorts out the problem, but it does not operate well with Jackson's (JSON parsing library) module "jackson-datatype-hibernate5", which skips hibernate lazy proxies when serializing the results.
Here is a scenario almost like the one I have that causes the problem:
Entities:
#Entity(name = "Book")
#Table(name = "book")
public class Book
implements Serializable {
#Id
#GeneratedValue
private Long id;
private String title;
private String author;
#NaturalId
private String isbn;
//Getters and setters omitted for brevity
}
#Entity(name = "Publication")
#Table(name = "publication")
public class Publication {
#Id
#GeneratedValue
private Long id;
private String publisher;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(
name = "isbn",
referencedColumnName = "isbn"
)
private Book book;
#Column(
name = "price_in_cents",
nullable = false
)
private Integer priceCents;
private String currency;
//Getters and setters omitted for brevity
}
Repository (Spring-Data, but you could try directly with the EntityManager):
#Repository
public interface PublicationRepository extends JpaReadRepository <Publication, Long>
{
#Query ("SELECT d FROM Publication d WHERE d.publisher = ?1 ")
Optional <Publication> findByPublisher (String isbn);
}
Thanks
The only way to achieve what you are looking for is by moving the annotatation #Id to the isbn property.
You can leave the #GeneratedValue on the autoincrement property.
Notes:
1 - Make sure that your equals/hc are following the OID(Object ID) on your domain case the "NaturalId" ISBN.
2 - It will be good to ensure if possible on DB level that your natural ID has unique contraint on it.

Complex select on EAV model Spring data jpa

I have a Product Entity like below (It's simple version)
#Entity
#Table(name = "product")
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#OneToMany(mappedBy = "product")
private List<ProductAtt> attributes;
}
Each Product could have one or more Attribute. Attribute look likes below
#Entity
#Table(name = "attribute")
public class Attribute {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
}
So I create a relation entity like below with extra value property
#Entity
#Table(name = "product_att")
public class ProductAtt implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#ManyToOne
#JoinColumn
private Product product;
#ManyToOne
#JoinColumn
private Attribute attribute;
private int value;
}
Now I want to find all products that have some attributes with custom values. For example all products that have attribute 1 with value 3 and attribute 2 with value 40 and ... .
What is the simplest and most efficient query to do that?
Since the number of attributes to query is not known at design time, one of the dynamic query mechanisms supported by Spring Data JPA will have to be used. The query can certainly be built using the JPA Specification or Criteria APIs.
If using QueryDSL support, subqueries with exists can be used. The following example shows how this can be done (assuming Java 8 and QueryDSL 4).
interface ProductRepository
extends CrudRepository<Product, Long>
, QueryDslPredicateExecutor<Product> {
default Iterable<Product> findAllByAttributes(final Map<String, String> attributes) {
final QProduct root = QProduct.product;
BooleanExpression query = root.isNotNull();
for (final String attribute : attributes.keySet()) {
final QProductAttribute branch = root.attributes.any();
final BooleanExpression subquery = branch.attribute.name.equalsIgnoreCase(attribute)
.and(branch.value.equalsIgnoreCase(attributes.get(attribute)));
query = query.and(JPAExpressions.selectFrom(QProductAttribute.productAttribute).where(subquery).exists());
}
return findAll(query);
}
}
It should be noted that the database design is such that performance problems are bound to happen, because the same table (ProductAttr) is included as many times as there are attributes to search by. This is not a problem of QueryDSL, JPA, Hibernate, SQL or the database server but the data model itself (also known as the EAV model).

Guidance on mapping the following relationship with ebean using jpa annotations

I have the following two entities in the DB ( structure is fixed ) which I am trying to map using JPA Annotations and EBEAN is ORM.
I have the following beans:
class Item {
public Long id;
public String name;
public Consignee intermediate;
public Consignee ultimate;
}
class Consignee {
public Long id;
public String name;
public String address;
public Item item;
}
And their corresponding tables:
Item
----
id
name
Consignee
---------
id:
name
address
item_id
type: [1,2] / 1: intermediate, 2:ultimate
the main entity is ITEM although the relationship is mapped from the consignee side.
How can I mapped this using the Annotations so that the consignees ( ultimate, intermediate ) are loaded when I fetch the Item object from DB ?
Could you please point me to the right direction
The two tables your are trying to map to Ebean are called Entity Models and the relation between Entity Consignee to Item is a One to Many Relationship.
Such relation can be mapped with a #OneToMany annotation on the Consignee side, and with an #ManyToOne on the Item side.
Also the field type of Consignee can be mapped with an Enumeration persisted as integer, and the remaining fields can be mapped via #Column annotation.
A possible implementation of your requirements could be something like:
public enum CONSIGNEE_TYPE {
INTERMEDIATE,
ULTIMATE
}
#Entity
#Table(name = "Consignee")
public class Consignee extends Model {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String name;
public String address;
#Enumerated(EnumType.ORDINAL)
public CONSIGNEE_TYPE type;
#Column(name="item_id")
#OneToMany(cascade = CascadeType.ALL, mappedBy = "consignee", fetch = FetchType.EAGER)
public List<Item> item = new ArrayList<Item>();
//TODO: Generate Constructors/Getters/Setters
}
#Entity
#Table(name = "Item")
public class Item extends Model {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String name;
#ManyToOne(optional = false, fetch = FetchType.EAGER)
public Consignee consignee = new Consignee();
//TODO: Generate Constructors/Getters/Setters
}
I removed the ambiguation with the ultimate and intermediate consignee type from the class Item since you can store the type of the consignee on the consignee itself.
Hope this helps.
As a very nice resource for the future, I recommend you to read the Unit Tests available on the source code of the Ebean itself. It ain't pretty but it helped me a lot!

Unidirectional OneToMany using Foreign Key Column

I want to maintain ACL information for each of my entities. For this i have the following entity:
#Entity
public class ACLEntry {
...
#Id
private Long id;
private Long sid;
private Integer permissionMask;
}
The entity for which the acl information should be maintained looks like the following:
#Entity
public class Folder {
...
#Id
private Long id;
#OneToMany
#JoinColumn(name="entity_id")
private List<ACLEntry> aclEntries;
}
From the hibernate docs:
A unidirectional one to many using a foreign key column in the owned entity is not that common
and not really recommended
Why is this not recommended and why should i use a jointable ?
Is there a better way to solve this ?
ACLEntry should have a reference to parent Folder #ManyToOne

Categories

Resources