spring data #ManyToOne #MapsId - spring-data-jpa

can some one help me to map this 3 entities correctly, because i have tried many solution but not working.
i have this 3 entities :
Product,Item and Pack that should be mapped like this:
Product entity
public class Product implements Serializable {
#OneToMany(mappedBy="parentProduct", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
#PrimaryKeyJoinColumn
private List<Pack> packListParent;
#OneToMany(mappedBy="childProduct", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
#PrimaryKeyJoinColumn
private List<Pack> packListChild;
}
Pack entity
public class Pack implements Serializable {
#EmbeddedId
protected PackPK PackPK;
#MapsId("itemId")
#JoinColumn(name = "item_id", referencedColumnName = "item_id", insertable = true, updatable = true)
#ManyToOne(fetch = FetchType.LAZY)
private Item item;
#MapsId("parentProductId")
#JoinColumn(name = "product_id", referencedColumnName = "product_id", insertable = true, updatable = true)
#ManyToOne(fetch = FetchType.LAZY)
private Product parentProduct;
#MapsId("childProductId")
#JoinColumn(name = "child_product_id", referencedColumnName = "product_id", insertable = true, updatable = true)
#ManyToOne(fetch = FetchType.LAZY)
private Product childProduct;
}
#Embeddable
public class PackPK implements Serializable {
#Basic(optional = false)
#Column(name = "product_id")
private Long parentProductId;
#Basic(optional = false)
#Column(name = "item_id")
private Long itemId;
#Basic(optional = false)
#Column(name = "child_product_id")
private Long childProductId;
}
Item entity
public class Item implements Serializable {
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
#PrimaryKeyJoinColumn
private List<Pack> PackList;
}
this mapping work in eclipselink but when i have migrated to spring data jpa with eclipselink i have this warning when i launch the server application.
This is the error message :
WARN - [AnnotationConfigServletWebServerApplicationContext]
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'produitService': Unsatisfied dependency expressed through field 'PackService';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'PackService': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class com.project.jpa.domain.PackPK]
on the existing id attribute [SingularAttributeImpl[EntityTypeImpl#1851998201:Item
[ javaType: class com.project.jpa.domain.Item descriptor: RelationalDescriptor(com.project.jpa.domain.Item --> [DatabaseTable(item)]), mappings: 47],
org.eclipse.persistence.mappings.ManyToOneMapping[item]]] on the identifiable type [EntityTypeImpl#1341697553:Pack [ javaType: class com.project.jpa.domain.Pack descriptor:
RelationalDescriptor(com.project.jpa.domain.Pack --> [DatabaseTable(pack_svod_content)]), mappings: 6]] but found attribute type [class com.project.jpa.domain.Item].

Related

How to save a copy of a entity

I use spring boot 3, I have this entity structure
#Data
#Entity
#Table
public class Repo {
#Id
#NotNull
private Long repoId;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "repo", cascade = {CascadeType.ALL})
private List<Pub> pubs = new ArrayList<>(0);
...
}
#Data
#Entity
#Table
public class Pub {
#Id
#Column
#NotNull
protected Long pubId;
#ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
#JoinColumn(name = "type_id")
protected TypeDoc typeDoc;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "pub", cascade = {CascadeType.ALL}, orphanRemoval = true)
protected List<Person> persons = new ArrayList<>(0)
#ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
#JoinTable(name = "pub_cat", schema = "", joinColumns = { #JoinColumn(name = "pub_id", nullable = true, updatable = false) }, inverseJoinColumns = { #JoinColumn(name = "cat_pub_id", nullable = true, updatable = false) })
private List<CatPub> catPub = new ArrayList<>(0);
}
A repo has a lit of pub, in reality they can have only 2.
So I feed a Pub object..... put it it the list of depot, and i need to create exactly the same and put it in the list of depot.
what is the most effective way to do it?

JPA #ManyToOne CascadeType.PERSIST

Hallo I have an entity with a #ManyToOne Relations ship
#Entity
public class TerminEntity extends AbstractEntity implements Serializable {
#ManyToOne(cascade = CascadeType.PERSIST)
#JoinColumn(name = "PERSON_ID")
private PersonEntity person;
#MappedSuperclass
public abstract class AbstractEntity implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "termineIdGenerator")
#SequenceGenerator(name="termineIdGenerator", sequenceName = "SEQ_TERMINVERWALTUNG")
private Long id;
When I try to persist a TerminEntity with an PersonEntity which is not already present in the database I get
java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: TerminEntity(person=PersonEntity
Why? What is wrong with the #ManyToOne(cascade = CascadeType.PERSIST) annotation?
Running on Oracle Weblogic 12c
I don't know why but after changing the annotation to
#ManyToOne(optional = false, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
#JoinColumn(name = "PERSON_ID", referencedColumnName = "ID", nullable = false)
the persist works.

Hibernate error: mappedBy reference an unknown target entity property

I am having an issue in setting up a many to many relationship in my entities. And I don't understand why
failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: cardgame.bean.User.card in cardgame.bean.Card.users
My Entities:
#MappedSuperclass
#Data
public class BaseEntity implements Serializable {
#Id
#Column(name = "id", nullable = false, unique = true)
private String id;
public BaseEntity() {
this.id = UUID.randomUUID().toString();
}
}
My user emtity:
#Data
#Entity
#Table(name = "users")
public class User extends BaseEntity {
#Column(name = "username", nullable = false, unique = true)
private String username;
#Column(name = "uuid", nullable = false)
private String uuid;
#Column(name = "email", nullable = false, unique = true)
private String email;
#OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Card> cards;
#Column(name = "isActive", nullable = false)
private boolean isActive;
}
My card entity:
#Data
#Entity
#Table(name = "cards")
public class Card extends BaseEntity {
#OneToMany(mappedBy = "card")
private List<User> users;
#Column(name = "strength", nullable = false)
private int strength;
#Column(name = "isActive", nullable = false)
private boolean isActive;
}
The users and cards tables have a many-to-many relationship via user_card table:
#Data
#Entity
#Table(name = "user_card")
public class UserCard implements Serializable {
#Id
#ManyToOne
#JoinColumn(name = "user_id", nullable = false)
private User user;
#Id
#ManyToOne
#JoinColumn(name = "card_id", nullable = false)
private Card card;
#Column(name = "cardCount", nullable = false)
private int cardCount;
}
What am i doing incorrect. Please help me

JPA filter entity nested list of objects

I'm Using JPA 2.1. I have 3 entities: Dr01 , Dr02 and Dr03 with the following structure:
public class Dr01 implements Serializable {
#OneToMany(cascade = CascadeType.ALL, mappedBy = "dr01")
private List<Dr02> dr02List;
}
public class Dr02 implements Serializable {
#OneToMany(cascade = CascadeType.ALL, mappedBy = "dr02")
private List<Dr03> dr03List;
#JoinColumn(name = "DR2CLM", referencedColumnName = "DR1CLM", insertable = false, updatable = false)
#ManyToOne(optional = false)
private Dr01 dr01;
}
public class Dr03 implements Serializable {
#JoinColumns({
#JoinColumn(name = "DR3CLM", referencedColumnName = "DR2CLM", insertable = false, updatable = false),
#JoinColumn(name = "DR3PTFN", referencedColumnName = "DR2PTFN", insertable = false, updatable = false)})
#ManyToOne(optional = false)
private Dr02 dr02;
private elementOBJ element;
}
public class elementOBJ implements Serializable {
#Column(name = "XXX")
private int id;
#Column(name = "YYY")
private int status;
}
I want to select from Dr01 and get only the Dr03 objects that have element objects which contains a value of 1 inside the status field.
How do I retrieve dr03List filtered by it's status value? (filtered not after the select).
Thank's In Advance.
Options that may be of assistance:
Create a DB view based on status of DR03 table and map your
entity to that.
Use JPA Inheritance using status as a
DiscriminatorColumn.
If using Hibernate use the non-JPA #Where
annotation to filter the collection

Insert object with composite primary key

I need help with this. With code is more clear, this is my function to persist:
public String finalizarCompra() {
Pedido pedido = new Pedido();
pedido.setEstado("almacen");
pedido.setFechaVenta(new Date());
pedido.setIdUsuario(loginBean.getUsuario());
Producto p;
Integer i;
DetPedido detPedido;
List<DetPedido> lista = new ArrayList<>();
for (Map.Entry e : productos.entrySet()) {
detPedido = new DetPedido();
p = (Producto) e.getKey();
i = (Integer) e.getValue();
detPedido.setProducto(p);
detPedido.setCantidad(i);
detPedido.setPrecioUnidad(p.getPrecioUnidad());
detPedido.setPedido(pedido);
lista.add(detPedido);
detPedidoBean.insert(detPedido);
}
pedido.setDetPedidoCollection(lista);
pedidoBean.insert(pedido);
return "";
}
This is my Pedido Entity:
#Entity
public class Pedido implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "ID_PEDIDO")
private Integer idPedido;
#Basic(optional = false)
#NotNull
#Column(name = "FECHA_VENTA")
#Temporal(TemporalType.TIMESTAMP)
private Date fechaVenta;
#Column(name = "FECHA_ENVIO")
#Temporal(TemporalType.TIMESTAMP)
private Date fechaEnvio;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 50)
#Column(name = "ESTADO")
private String estado;
#JoinColumn(name = "ID_USUARIO", referencedColumnName = "ID_USUARIO")
#ManyToOne(optional = false)
private Usuario idUsuario;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "pedido")
private Collection<DetPedido> detPedidoCollection;
// Getters and Setters //
This is my DetPedido Entity:
#Entity
public class DetPedido implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected DetPedidoPK detPedidoPK;
#Basic(optional = false)
#NotNull
#Column(name = "CANTIDAD")
private Integer cantidad;
#Basic(optional = false)
#NotNull
#Column(name = "PRECIO_UNIDAD")
private Double precioUnidad;
#JoinColumn(name = "ID_PRODUCTO", referencedColumnName = "ID_PRODUCTO", insertable = false, updatable = false)
#ManyToOne(optional = false)
private Producto producto;
#JoinColumn(name = "ID_PEDIDO", referencedColumnName = "ID_PEDIDO", insertable = false, updatable = false)
#ManyToOne(optional = false)
private Pedido pedido;
// Getters and Setters //
And this is my DetPedidoPK:
#Embeddable
public class DetPedidoPK implements Serializable {
#Basic(optional = false)
#NotNull
#Column(name = "ID_PEDIDO")
private Integer idPedido;
#Basic(optional = false)
#NotNull
#Column(name = "ID_PRODUCTO")
private Integer idProducto;
// Getters and Setters //
The Entities ara generated automatically from the Database, also DetPedidoPK, and now I don't know how to save a Pedido. I tried with the code above, but it doesn't work.
Can anybody help me?
Greetings.
If you are using JPA 1.0 and this entity model, then you will need to persist and flush both Producto and Pedido instances to have their IDs assigned before you can persist the DetPedido instance that will reference them. Once that is done, you will need to manually set the id values in DetPedido's DetPedidoPK instance so that they match the referenced Producto and DetPedido key values. You cannot insert DetPedido without the DetPedidoPK values having been set.
JPA 2.0 supports derived IDs, which allows marking the relationship as either #ID or #MapsId, indicating that the ID values should be pulled from the joincolumn associated to the relationship. In this case, it would become:
#ManyToOne(optional = false)
#MapsId("idProducto")
private Producto producto;
#ManyToOne(optional = false)
#MapsId("idPedido")
private Pedido pedido;
If you wanted, you could do away with the embeddable within DetPedido and just mark the relationships as the #Id, and because it is composite you would use the DetPedidoPK as the PK class.