deleting parent exception with jpa - jpa

With netbeans 8.1 I have generated entity and jpa controller.
I have a parent entity and a child with a one to one relation.
The child entity has primary key equals to foreign key to the parent:
#Entity
#Table(name = "parent")
#XmlRootElement
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#OneToOne(cascade = CascadeType.ALL, mappedBy = "parent")
private Child child;
public Parent() {
}
etc....
}
#Entity
#Table(name = "child")
#XmlRootElement
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#Column(name = "parent_id")
private Integer parentId;
#JoinColumn(name = "parent_id", referencedColumnName = "id", insertable = false, updatable = false)
#OneToOne(optional = false)
private Parent parent;
public Child() {
}
etc....
}
When I try to delete the parent I get:
IllegalOrphanException: This Parent (model.Parent[ id=1 ]) cannot be destroyed since the Child model.Child[ parentId=1 ] in its child field has a non-nullable parent field.
To avoid this I should delete the child first, but due to cascade.ALL it should be already done automatically.

Coding Child class as follows works
#Entity
#Table(name = "child")
#XmlRootElement
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#JoinColumn(name = "parent_id", referencedColumnName = "id")
#OneToOne(optional = false)
private Parent parent;
...
}
Tables look like
create table child (parent_id integer not null,..., primary key (parent_id))
create table parent (id integer not null, ..., primary key (id))
alter table child add constraint ... foreign key (parent_id) references parent
The removal of a parent (em.remove(parent)) leads to
delete from child where parent_id=?
delete from parent where id=?

Related

Pagerequest object with sort property of the child

How to specify the children's property name while creating the PageRequest object. In the below example, I want to sort based on the name property of the Customer entity which is part of the Order class. I should be able to sort on any of the parent or child field.
Sort.Direction sortDirection = direction
.equalsIgnoreCase("asc") ? Sort.Direction.ASC : Sort.Direction.DESC;
//how to make the `name` property map to `Customer` name
PageRequest pageRequest = PageRequest.of(page, size, sortDirection, "name");
Page<Order> pageResponse = this.orderRepository.findAll(pageRequest);
#Entity
#Table(name = "orders")
public class Order {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
#JoinColumn(name="customer_id", nullable = false)
#JsonBackReference
private Customer customer;
...
----------
#Entity
#Table(name = "customers")
#NoArgsConstructor
public class Customer implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotEmpty(message = "customer name cannot be empty")
private String name;
#OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JsonManagedReference
private Set<Order> orders;
...

JPA Mapping OneToMany with partial embedded id child

Simple example (hopefully). I have a primary key (using a sequence) in one table and that value is a partial FK into a child table. I see the Parent is trying to be saved with a generated sequence, but then I see an exception that the parentId in the embeddable is null while saving the child. The sequence value used for the parent is not being carried over to the child. I have tried many annotations and mappedBy/join column names but no luck.
Any pointers would be very much appreciated.
public class Parent {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "audit_seq")
#SequenceGenerator(name = "audit_seq", allocationSize = 5)
private Long id;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
private List<Child> childList = new ArrayList<>();
//Used to add child record o the parent
public void addChild(Child child) {
this.childList .add(child);
child.setParent(this);
}
}
#Embeddable
public class ChildId {
private Long parentId;
private String name;
}
public class Child {
#EmbeddedId
private ChildId id;
private String myCol;
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name = "parentId", insertable = false, updatable = false)
private Parent parent;
}
I was able to get this resolved with the use of a couple of annotations:
Parent class:
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
#PrimaryKeyJoinColumn
private List<Child> childList = new ArrayList<>();
Child class:
#ManyToOne
#MapsId("id")
#JoinColumn(name = "id")
private Parent parent;
Now all objects are being persisted when saving the parent with the appropiate sequence id.

Foreign key is inserted null with CascadeType.PERSIST,- Spring Data Jpa

I have two tables Parent and Child. They have one to one relation between them. When I save the Parent table it should insert into the Child table as well. A child table is inserted but the foreign key is null in the child table. I want a foreign key to be the autogenerated value from the Parent table.
I am using the Postgres database.
Here are my entity relations:
#Table(name = "`PARENT`")
#Setter
#Getter
#Entity
public class Parent{
#Id
#Column(name = "`RPT_PARM_ID`")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//Other column mappings.
#OneToOne(mappedBy = "parent", cascade = { CascadeType.DETACH, CascadeType.MERGE,
CascadeType.REFRESH, CascadeType.PERSIST })
private Child child;
}
#Entity
#Table(name = "`CHILD`")
#Setter
#Getter
public class Child {
#Id
#Column(name = "`ID`")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//Other column mappings
#OneToOne(optional = false,cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST })
#JoinColumn(name = "`PARAM_ID`", referencedColumnName ="`RPT_PARM_ID`" )
private Parent parent;
}
//This is how I save the code
pubilc void save(){
Parent parent = new Parent();
//set other fields for parent.
Child child =new Child();
//set other fields in child.
parent.setChild(child);
repository.save(parent);
}
In child table, PARAM_ID is inserted null, but I want the column value from RPT_PARM_ID (this is auto-generated)

How to name Foreign Key with #CollectionTable in JPA?

I need help with resolving our problem with naming FK in JPA. We have one embeddable entity e.g. Foo which is used as collection in another one entity Bar.
embeddable entity:
#Embeddable
public class Foo{
#Column(name = "foo1")
private String foo1;
#Column(name = "foo2")
private String foo2;
}
main entity:
#Entity
#Table(name = "Bar")
public class Bar {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
#Id
private Long id;
#ElementCollection
#CollectionTable(name = "foos", joinColumns = #JoinColumn(name = "bar_id", foreignKey = #ForeignKey(name = "foo_bar_fk"), nullable = false))
private List<Bar> bars;
}
When I generated tables in database (postgres) foreign key is called fdk44l345k64 instead foo_bar_fk. Can you tell me how to fix it? Thanks.

Exception while persisting JPA object in DB having one to many relation

hi have two tables in picture table a and table b as follows :
#Entity
#Table(name = "A")
public class A implements Serializable {
#Id
#SequenceGenerator(name = "JOURNAL_CATEGORY_ID_GENERATOR", allocationSize = 1, sequenceName = "clm_jounal_category_config_seq")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "JOURNAL_CATEGORY_ID_GENERATOR")
#Column(name = "CLAIM_ID")
private String claimId;
#Column(name = "name")
private String name;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
private List<ClaimDTLS> claimDetails;
}
B Primary Key:
#Embeddable
public class BPK implements Serializable {
#Column(name = "code")
private String code;
#Column(name = "CLAIM_ID")
private String claimId;
}
B Entity:
#Entity
#Table(name = "B")
public class B implements Serializable {
#EmbeddedId
protected BPK bpk;
#Column(name = "name")
private String name;
#MapsId("country_code")
#JoinColumn(name = "claimId", referencedColumnName = "claimId", insertable = false, updatable = false)
#ManyToOne
private A a;
}
when i try to persist object of A type in Db the value of table b claim id is not set and is intialized with zero.
Also primary key of table A is generated with a oracle sequence.
any help will be welcomed.
thanks in advance
Sequence values are numbers and when JPA use them as a generator it call the setter method of the entity PK. Now, you defined your PK as a string while you use a sequence and so no matching setter can be found. Change the type of you PK to be Long and things shall work