Is it possible to not persist associated entities?
I have a table
create table public.B(
id character varying(30) not null,
name character varying(255) not null,
a_id character varying(6) not null,
primary key (id, a_id),
foreign key (a_id) references public.A (id)
match simple on update no action on delete no action
);
With This entity
@Entity(name = "B")
@IdClass(BId.class)
public class B {
@Id
@NotBlank
public String id;
@NotBlank
public String name;
@Id
@ManyToOne(optional = false)
@JoinColumn(name = "a_id", nullable = false)
}
And associated Entity
@Entity(name = "A")
public class A {
@Id
@NotBlank
public String id;
@NotBlank
public String name;
}
I just want persist the entity B and the foreign key but not persist the entity A
Example
void myfunction(final String aId){
if (aId != null) {
var a = new A();
a.id = aId;
var b = new B();
b.id = "pop";
b.name = "pop";
b.a = a;
entityManager.persist(b);
}
}
I want to persist b without create A in database (because it already exists In database). Id just want persist in column a_id the id.
It ‘s work if before I retrieve the entity A over entityManager but it’s doing a request in database useless in my case. I just want to instert.