Hibernate one to many mapping for multiple tables - java

#Entity
#Table(name="Visit")
public class Visit {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "v_id_seq", sequenceName = "v_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "v_id_seq")
#Column(name = "id")
private Long id;
#OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
#OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;
#Entity
#Table(name="test")
public class Test {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "t_id_seq", sequenceName = "t_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "t_id_seq")
#Column(name = "id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JsonBackReference
#JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit visit;
#Entity
#Table(name="direction")
public class directions {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "d_id_seq", sequenceName = "d_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "d_id_seq")
#Column(name = "id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JsonBackReference
#JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit Visit;
Hello i am new to hibernate
I am trying to map OneToMany Visit-->Test and Visit-->direction but getting error
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property:
one visit can have multiple direction and test
how can i implement this?
plz help me!

The value of the mappedBy field on the #OneToMany annotation references java instance variable names, and it is case sensitive. You are setting it to Visit, but in the directions and test classes the variable names are visit.
The solution is to change property mappedBy from Visit to visit (lower case V):
#OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
#OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;

Related

JPA TypedQuery doesn't return results with given condition

I have the following models:
#Entity
#Table(name = "product")
public class Product implements Serializable {
#Id
#GeneratedValue(generator = "product_id_seq", strategy = GenerationType.SEQUENCE)
#SequenceGenerator(name = "product_id_seq", sequenceName = "product_id_seq", allocationSize = 1)
#Column(name = "id")
private Long id;
#OneToMany(mappedBy = "product")
private List<ProductContent> productContent;
#Column(name = "expired_at", nullable = false)
#Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date expiredAt;
#Column(name = "created_at", nullable = false)
#Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date createdAt;
#Entity
#Table(name = "product_content")
public class ProductContent {
#Id
#GeneratedValue(generator = "product_content_id_seq", strategy = GenerationType.SEQUENCE)
#SequenceGenerator(name = "product_content_id_seq", sequenceName = "product_content_id_seq", allocationSize = 1)
#Column(name = "id")
private Long id;
#Column(name = "title", nullable = false)
private String title;
#Column(name = "content", columnDefinition="TEXT", nullable = false)
private String content;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "product_id")
private Product product;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "supported_locale_id")
private SupportedLocale locale;
#Entity
#Table(name = "supported_locale")
public class SupportedLocale {
#Id
#GeneratedValue(generator = "supported_locale_id_seq", strategy = GenerationType.SEQUENCE)
#SequenceGenerator(name = "supported_locale_id_seq", sequenceName = "supported_locale_id_seq", allocationSize = 1)
#Column(name = "id")
private Long id;
#Column(name = "language_name", nullable = false)
private String languageName;
#Column(name = "language_tag", nullable = false)
private String languageTag;
#OneToMany(mappedBy = "locale")
private List<ProductContent> productContent;
I want to fetch all products in english language. I'm using following typed query:
List<Product> products = em.createQuery("SELECT p FROM Product p JOIN p.productContent pc JOIN pc.locale l WHERE l.languageName=:languageName", Product.class)
.setParameter("languageName", "English")
.getResultList();
It returns me list of products not limited to English language(there are spanish productContent for example). I also tried to use the generated SQL query directly on database (Postgres) and it works fine (It returns only products with english language). How could I solve it?

Mapping exception: Unable to find column with logical name

Ive having problems with my hibernate program. I have deleted and rewrited the class and still throws same error. I dont have declared the cod_modulo in this table, and even if i remove the one to many parameter, still throws same error. I dont understand anything.
Here the classes referenced. It also has getters and setters for all elements and the empty public constructor.
The modulo class
#Entity
#Table(name="modulo")
public class Modulo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "SEQ_MODULO", sequenceName = "SEQ_MODULO", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MODULO")
private int cod_modulo;
#Column(name = "ancho_modulo")
private Double ancho_modulo;
#Column(name = "largo_modulo")
private Double largo_modulo;
#Column(name = "alto_modulo")
private Double alto_modulo;
#Column(name = "orden")
private Integer orden;
#ManyToOne(cascade = CascadeType.DETACH, targetEntity = Lineal.class, fetch = FetchType.EAGER)
#JoinColumn(name = "cod_lineal", referencedColumnName = "cod_lineal")
private Lineal lineal;
//#OrderBy
#JsonProperty(access = Access.WRITE_ONLY)
#OneToMany(mappedBy = "modulo", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Balda> baldas;
The lineal class
#Entity
#Table(name="lineal")
public class Lineal implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "SEQ_LINEAL", sequenceName = "SEQ_LINEAL", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LINEAL")
private int cod_lineal;
#ManyToOne(cascade = CascadeType.DETACH, targetEntity = Seccion.class, fetch = FetchType.EAGER)
#JoinColumn(name = "cod_seccion", referencedColumnName = "cod_seccion")
private Seccion seccion;
#JsonProperty(access = Access.WRITE_ONLY)
#OneToMany(mappedBy = "lineal", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Modulo> modulos;
#Column(name = "refrigerado")
private Boolean refrigerado;
#Column(name = "horizontal")
private Boolean horizontal;
#Column(name = "pos_x")
private Double pos_x;
#Column(name = "pos_y")
private Double pos_y;
The error:
Mapping exception: Unable to find column with logical name: cod_modulo in org.hibernate.mapping.Table(lineal) and its related supertables and secondary tables
Try adding #Column for your cod_modulo property like so:
#Id
#SequenceGenerator(name = "SEQ_MODULO", sequenceName = "SEQ_MODULO", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MODULO")
#Column(name = "cod_modulo")
private int cod_modulo;

OneToMany PersistentBag

I have a problem with storing OneToMany relationship with Hibernate.
What I got is AdvertisementData entity which looks as follows:
#Entity
#Table(name = "advertisement_data")
public class AdvertisementData {
#Id
#Column(name = "order_id", unique = true, nullable = false)
#GeneratedValue(generator = "gen")
#GenericGenerator(name = "gen", strategy = "foreign", parameters = #Parameter(name = "property", value = "order"))
private Long id;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE, mappedBy = "advertisementData")
private List<KRPData> krpData;
//setters and getters
}
and KRPData which is defined as follows:
#Entity
public class KRPData {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String description;
#Type(type = "org.hibernate.type.SerializableToBlobType")
private List<String> images;
private String section;
#Type(type = "org.hibernate.type.SerializableToBlobType")
private List<String> filenames;
#ManyToOne
private AdvertisementData advertisementData;
//getters and setters
}
I can see that both entities are stored, however every time I fetch AdvertisementData, the results looks like:
KRPData is returned as PersistentBag without any data.
Hibernate version: 4.2.2.Final
Any ideas what could be the case?
Thank You in advance!

Hibernate ORA-02292: integrity constraint (ROOT.SYS_C007062) violated - child record found

I following have hibernate entities:
#Entity
#Table(name = "News")
public final class News implements Serializable, IEntity {
private static final long serialVersionUID = 3773281197317274020L;
#Id
#SequenceGenerator(name = "NEWS_SEQ_GEN", sequenceName = "NEWS_SEQ")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "NEWS_SEQ_GEN")
#Column(name = "NEWS_ID", precision = 0)
private Long newsId; // Primary key
#Column(name = "TITLE")
private String title;
#Column(name = "SHORT_TEXT")
private String shortText;
#Column(name = "FULL_TEXT")
private String fullText;
#Temporal(TemporalType.DATE)
#Column(name = "CREATION_DATE")
private Date creationDate;
#Temporal(TemporalType.DATE)
#Column(name = "MODIFICATION_DATE")
private Date modificationDate;
#OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
#JoinColumn(name = "NEWS_ID", updatable = false, referencedColumnName = "NEWS_ID")
#OrderBy("creationDate ASC")
private List<Comment> commentsList;
#ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable(name = "NEWS_TAG", joinColumns = { #JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { #JoinColumn(name = "TAG_ID") })
private Set<Tag> tagSet;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable(name = "NEWS_AUTHOR", joinColumns = { #JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { #JoinColumn(name = "AUTHOR_ID") })
private Set<Author> author;
And the second:
#SequenceGenerator(name = "COMMENTS_SEQ", sequenceName = "COMMENTS_SEQ")
#Entity
#Table(name = "Comments")
public class Comment implements Serializable, IEntity {
private static final long serialVersionUID = 3431305873409011465L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMMENTS_SEQ")
#Column(name = "COMMENT_ID", precision = 0)
private Long commentId; // Primary key
#Column(name = "NEWS_ID")
private Long newsId;
#NotEmpty
#NotNull
#Column(name = "COMMENT_TEXT")
private String commentText;
#Temporal(TemporalType.DATE)
#Column(name = "CREATION_DATE")
private Date creationDate;
When I'm trying to remove entity News, I get the exception ORA-02292: integrity constraint (ROOT.SYS_C007062) violated - child record found. So, if I remove the property "updatable = false" it tries to set nullable fields into property Comment. What is my mistake? Please, help.
Thanks.
Because your news records have a one to one or one to many relation with comments. You most likely did not specifcy a CACASDE ON DELETE clause while defining your table. in order to delete entity NEWS you have to make sure that all of its related comments records are deleted or are referencing another NEWS record.
basicaly the definition of the ORA 02292 exception.

JPA cascade merge does not persist attribute

I've an entity on OpenJPA 2.0
#Entity
#Table(name = "os_wfentry")
#SequenceGenerator(name = "jwe_seq", sequenceName = "jwe_seq", initialValue = 10, allocationSize = 1)
public class JPAWorkflowEntry implements WorkflowEntry, Serializable {
private static final long serialVersionUID = -755511983025049452L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jwe_seq")
private long id;
#Column(name = "name")
private String workflowName;
#Column(name = "state")
private Integer workflowState;
#Column(name = "version")
private Integer version;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "entry")
private final List<JPACurrentStep> currentSteps;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "entry")
private final List<JPAHistoryStep> historySteps;
public JPAWorkflowEntry() {
currentSteps = new ArrayList<>();
historySteps = new ArrayList<>();
}
...
and on JPACurrent and JPAHistory step I've inserted:
#ManyToOne
#Column(name = "entry_id")
protected JPAWorkflowEntry entry;
It is all correct (in theory); but when I try to save (or update) a new instance of JPAWorkflowStore, having a NOT EMPTY list of (current or history) steps, list of steps attribute is not persistend on db and it always an empty list. Can You help me?? What am I doing wrong??
You need to specify #JoinColumn(name = "id", nullable = false) for your JPACurrent and JPAHistory.
What you have done is #Column(name = "entry_id") i dont see "entry_id" mapping to any cölumn in JPAWorkflowEntry

Categories

Resources