Map (integer integer) mapping in hibernate - hibernate-mapping

How would you annotate this bean to be mapped in hibernate ?
#Entity
public class PerformanceValues implements Serializable{
private static final long serialVersionUID = 1234850675335166109L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//key is mass, value is distance needed
private Map<Integer, Integer> massToDist;
}
Each performanceValues entity has a unique map, and each map can be related to only one PerformanceValues (I guess this is a oneToOne relationship)
Thanks

Related

Split huge sql table logically into smaller tables

I'm making a Spring boot application with Hibernate ORM framework.
I have Employee entity there:
#Entity
public class Employee {
private String firstName;
private String position;
//// more than 30 private fields
//// fields related to one sublogic
private String category;
private LocalDate categoryAssignmentDate;
private LocalDate categoryAssignmentDeadlineDate;
private LocalDate docsSubmitDeadlineDate;
}
There are more than 30 private fields in Employee class.
And as you can see, I have 4 fields related to same sublogic Category.
So my question is: Is it a good practise to split my Employee entity into two entities Employee and Category, which will be connected as OnetoOne relationship?
Does it make the code clearer?
Use embedded and embeddable to prevent double table mapping and unnecessary OneToOne relations.
#Entity
public class Employee {
private String firstName;
private String position;
#Embedded
private Category category
}
#Embeddable
public class Category{
private String category;
private LocalDate categoryAssignmentDate;
private LocalDate categoryAssignmentDeadlineDate;
private LocalDate docsSubmitDeadlineDate;
}
You might need to add attribute overrides

Deadlock when we we have dependent entities in hibernate and try to update table using multiple threads in Vertica

I have two java classes. Father.java and Children.java
#Entity
#Table(name = "FATHER")
#JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
class Father implements Cloneable
{
#Id
#Column(name = "father_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long fatherId;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "father_id")
#Fetch(value = FetchMode.SUBSELECT)
private List<Children> children = new ArrayList<Children>();
//getter and setters and public constructors
}
#Entity
#Table(name = "Children")
class Children implements Comparable<Children>
{
#JsonIgnore
#Id
#Column(name = "children_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long child_id;
#JsonIgnore
#Column(name = "father_id")
private long fatherId;
//public constructors and getters and setters
}
public interface RelationDao{
public Father update() throws Exception;
}
#Repository("relationDao")
#EnableTransactionManagement
#Transactional
public RelationDaoImpl{
#Override
#Transactional("txManager")
public Father update(Father father)
{
father = merge(father);
//added retry logic as well also father is updated with a new child which is why merge
}
}
I receive the Deadlock X exception if several threads visit the same table (entity father) to updates with distinct row entries, even though the records are different.
Is there any way to fix why the entire table locks up instead than just one row?
Even though I haven't updated or added anything to the code, the transaction isolation level is SERIALIZABLE.
DB system is Vertica
Explained here, if anyone is coming here to check why Vertica doesn’t support row level locks for updates or delete. https://stackoverflow.com/a/69917383/8799862
So I used synchronized to perform thread-safe updates and deletes.

One to one with referencedColumnName mapping on wrong field

I'm facing problem on one to one relationship. I have 2 tables, one Article which has 1 FK "FICHE_ID" refrences to the second table's id Fiche(ID_FICHE) and the problem is that JPA is not mapping on the right field, it's taking ID_ARTICLE to map ID_FICHE instead of FICHE_ID.
This is the code below :
#Entity
#Table(name="ARTICLE")
public class Article implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="ID_ARTICLE")
Integer id=0;
#Column(name="ENTREPRISE")
private String entreprise;
#Column(name="CODE_ARTICLE")
private String code;
#Column(name="LIBELLE_ARTICLE")
private String libelle;
#Column(name="ROLE_READ")
private String role;
#Column(name="PRIX")
private int prix;
#Column(name="OBLIGATOIRE")
private String obligatoire;
#NaturalId
#Column(name="TAILLE_CODE")
private String tailleCode;
#NaturalId
#Column(name="FICHE_ID")
private Integer ficheId;
#OneToOne(fetch = FetchType.EAGER,mappedBy="article")
FicheArticle fiche;
And
#Entity
#Table(name="FICHE")
public class FicheArticle {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="ID_FICHE",insertable = false,updatable = false)
private Integer id=0;
#Lob
#Column(name="FICHE",insertable = false,updatable = false)
private byte[] fiche;
#Lob
#Column(name="FICHE")
private Blob ficheBlob;
#Column(name="ENTREPRISE")
private String entreprise;
#OneToOne
#JoinColumn(name="ID_FICHE", referencedColumnName = "FICHE_ID")
private Article article;
Please, can you help me ?
I am not positive I understand your question; but it looks like a few things need to be changed. Since the foreign key is held by the ARTICLE table, you cannot use mappedBy on the Article.fiche mapping. Instead, you should specify the appropriate join column on Article.fiche and mappedBy on Fiche.article:
#Entity
#Table(name = "ARTICLE")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID_ARTICLE")
Integer id = 0;
// ...
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "FICHE_ID", referencedColumnName = "ID_FICHE")
FicheArticle fiche;
And
#Entity
#Table(name = "FICHE")
public class FicheArticle {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID_FICHE", insertable = false, updatable = false)
private Integer id = 0;
// ...
#OneToOne(mappedBy = "fiche")
#JoinColumn(name="ID_FICHE", referencedColumnName = "FICHE_ID")
private Article article;
Also, I'm guessing Article.ficheId is not a #NaturalId for Article; so you should simply remove the field (and its mapping).

Entry and Serialisation

My project is in the form:
Class Persistant :
#Entity
public class Produit implements Serializable {
private static final long serialVersionUID = -3352484919001942398L;
#Id
#GeneratedValue
private Long id;
private String module;
private String produit;
//getter&&setter
Class Dao
public List<Entry<Integer, List<Produit>>> parProduit(String cat) {
.......
HashMap<Integer, List<Produit>> legaux = new HashMap<Integer, List<Produit>>();
........
List<Map.Entry<Integer, List<Produit>>> entries = new ArrayList<Entry<Integer, List<Produit>>>(legaux.entrySet());
return entries;
}
when i execute this code i get this error :
java.io.NotSerializableException: java.util.HashMap$Node
java.util.HashMap.EntrySet<K, V>
is not serializable.
legaux.entrySet()
probably returns set of type java.util.HashMap.EntrySet, you may want to check that.

JPQL JOIN doesn't return results

My JPQL JOIN query doesn't return any results.
I'm trying to join TargetBean and TipBean on the TargetBean primary key:
public class TargetBean implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Entity
public class TipBean implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToOne
#PrimaryKeyJoinColumn
private TargetBean target;
My query looks like this:
List<Object[]> tipList =
(List<Object[]>)em.createQuery("SELECT tb.result, tg.actualResult "
+ "FROM TipBean tb JOIN tb.target tg")
.getResultList();
Do you notice something wrong with this query or what goes wrong?
Try removing the #PrimaryKeyJoinColumn annotation, I don't think it is appropriate in this context (it is used with inheritence).