so until now I have used a remote server and database and JPA has always created those tables:
but now I have to do something in a local PostgreSQL database and JPA creates only the Entities:
I run the same code. The only thing I have changed is the persistance unit in the Entitimanagerfactory.
Do you guys have any idea why this happens?
Also... I am not posting any code since I don't know where the issue might be and I don't want to post my entire program.
Edit:
#ManyToMany(targetEntity = Question.class, cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
#JoinTable(name = "test",
joinColumns = #JoinColumn(name = "gameId", referencedColumnName = "gameId"),
inverseJoinColumns = #JoinColumn(name = "questionId", referencedColumnName = "questionId"),
uniqueConstraints = #UniqueConstraint(columnNames = {"questionId", "gameId"})
)
private List<Question> questions = new ArrayList<>();
#ManyToMany(targetEntity = Game.class, cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
#JoinTable( name = "test",
joinColumns = #JoinColumn(name = "questionId", referencedColumnName = "questionId"),
inverseJoinColumns = #JoinColumn(name = "gameId", referencedColumnName = "gameId"),
uniqueConstraints = #UniqueConstraint(columnNames = {"questionId", "gameId"})
)
private List<Game> game = new ArrayList<>();
Related
to-many tables mapped like this :
Resume.java :
#ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
#JsonIgnore
#JoinTable(
name = "resume_skills",
joinColumns = #JoinColumn(name = "skill_id"),
inverseJoinColumns = #JoinColumn(name = "resume_id"))
private List<Skill> skills = new ArrayList<Skill>();
public void addSkill(Skill skill) {
skills.add(skill);
skill.getResumes().add(this);
}
Skill.java
#ManyToMany(mappedBy="skills")
#JsonIgnore
private List<Resume> resumes;
I do this :
Resume resumeToAdd = new Resume(resume.getGithubAdress(),
resume.getLinkedinAdress(),
resume.getCoverLetter(),
resume.getPicture(),
employee
);
resumeDao.save(resumeToAdd);
Skill skill = skillService.findById(1).getData();
System.out.println(skill);
resumeToAdd.addSkill(skill);
resumeDao.save(resumeToAdd);
But it occurs an error, for example it added a Resume with id 60 and it's trying to add mapping with skill id 60 But it cant because there is no skill in database with id of 60, it should be 1. What did i miss here?
Looks like you mixed up the order of the mapping. It should be this:
#JoinTable(
name = "resume_skills",
joinColumns = #JoinColumn(name = "resume_id"),
inverseJoinColumns = #JoinColumn(name = "skill_id"))
There is an entity call Version and this entity has an inner join many-to-many relationship. The bridge table VER_EQUIVALENTS has columns [VER_ID, EQUIVALENT_VER_ID, CODE] and the CODE column contains the code of the equivalent version.
This is how I implemented this relationship.
public class Version {
private String code;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(
name = "VER_EQUIVALENTS",
joinColumns = {#JoinColumn(name = "VER_ID")},
inverseJoinColumns = {#JoinColumn(name = "EQUIVALENT_VER_ID")}
)
#MapKey(name = "code")
#org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
private Map<String, Version> equivalentVersions = new HashMap<String, Version>();
}
This is what I got when I try to insert a version contains equivalent versions.
ORA-01400: cannot insert NULL into ("VER_EQUIVALENTS"."CODE")
What can I do to set the CODE value?
If I correctly understand that you need, you should correct your mapping in the following way:
public class Version {
// ...
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(
name = "VER_EQUIVALENTS",
joinColumns = {#JoinColumn(name = "VER_ID")},
inverseJoinColumns = {#JoinColumn(name = "EQUIVALENT_VER_ID")}
)
#MapKeyColumn(name = "CODE")
private Map<String, Version> equivalentVersions = new HashMap<String, Version>();
}
I found here a good article about designing the many-to-many schema with JPA.
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "student_subject", joinColumns = #JoinColumn(name = "student_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "subject_id", referencedColumnName = "id"))
private Set<Subject> subjects;
My question is, can I map a start schema, for example on JoinColumn I want to add multiple tables. It is possible?
I have this relationship in my database:
And I want to be able to create an order, that contains more products (as well more instances of the same product). Nevertheless I am able to have just one row in the order_has_product table for every single order_id. Moreover, I tried to load the data via lazyloading, but it won't work and it makes the whole app to run really slowly. Here are definitions of the relationship in my entities:
OrderEntity:
#ManyToMany(fetch = FetchType.LAZY)
#Cascade({org.hibernate.annotations.CascadeType.ALL})
#JoinTable(name = "order_has_product",
joinColumns = {#JoinColumn(name = "order_id", nullable = false, updatable = false)},
inverseJoinColumns = {#JoinColumn(name = "product_id", nullable = false, updatable = false)})
private List<ProductEntity> products;
ProductEntity:
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "products")
private List<OrderEntity> orders;
Any idea how to fix lazyloading and to make it possible to have more products in order?
I have ManyToMany mapping like this:
#XmlTransient
#ManyToMany(cascade = {CascadeType.ALL})
#JoinTable(name = "users_clients",
joinColumns = {#JoinColumn(name = "user_id")},
inverseJoinColumns = #JoinColumn(name = "client_id"))
public List<Client> getClients() {
return clients;
}
And other side:
#ManyToMany(mappedBy = "clients")
private List<User> users = new ArrayList<User>();
So as you can see I have JoinColumn name = user_id and client_id but hibernate mapps this columns with names userS_id and clientS_id as their tables names. Why that happening? Any suggestions?
Try specifying the column name in your #JoinColumn annotation. Also you were missing braces around the inverseJoinColumns
#JoinTable(name = "users_clients",
joinColumns = {
#JoinColumn(name = "user_id", referencedColumnName="user_id")},
inverseJoinColumns = {
#JoinColumn(name = "client_id", referencedColumnName="client_id")})