Managing many to many relationship with hibernate












0















I am making simple spring mvc web app and I am using hibernate with oracle. I have two models - User and role and I want to define many to many relationship between them. I have seen several tutorials and have defined my models as shown below:



This is User class:



@Entity
@Table(name = "AppUser")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
@Column(name = "Id")
private int id;

@Column(name = "Username")
private String username;

@Column(name = "Password")
private String password;


@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}
)
@JoinTable
(
name = "user_roles",
joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }
)
private Set<Role> roles = new HashSet<Role>(0);

//getters and setters....
}


This is Role class:



@Entity
@Table(name = "role")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
private int id;

private String role;

@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "roles"
)
private Set<User> users;

//getters and setters....
}


When I run this app on server I get the following error:



enter image description here



This error says that table doesn't exist, so I am interested in should I create it manually(I do not think so) or am I missing anything here?










share|improve this question























  • what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

    – guleryuz
    Nov 25 '18 at 18:49











  • I have this in my configuration file: hibernate.hbm2ddl.auto=update

    – Alexander Mujirishvili
    Nov 25 '18 at 18:57











  • The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

    – Bob Jarvis
    Nov 25 '18 at 19:11











  • At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

    – Alexander Mujirishvili
    Nov 25 '18 at 19:15


















0















I am making simple spring mvc web app and I am using hibernate with oracle. I have two models - User and role and I want to define many to many relationship between them. I have seen several tutorials and have defined my models as shown below:



This is User class:



@Entity
@Table(name = "AppUser")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
@Column(name = "Id")
private int id;

@Column(name = "Username")
private String username;

@Column(name = "Password")
private String password;


@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}
)
@JoinTable
(
name = "user_roles",
joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }
)
private Set<Role> roles = new HashSet<Role>(0);

//getters and setters....
}


This is Role class:



@Entity
@Table(name = "role")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
private int id;

private String role;

@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "roles"
)
private Set<User> users;

//getters and setters....
}


When I run this app on server I get the following error:



enter image description here



This error says that table doesn't exist, so I am interested in should I create it manually(I do not think so) or am I missing anything here?










share|improve this question























  • what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

    – guleryuz
    Nov 25 '18 at 18:49











  • I have this in my configuration file: hibernate.hbm2ddl.auto=update

    – Alexander Mujirishvili
    Nov 25 '18 at 18:57











  • The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

    – Bob Jarvis
    Nov 25 '18 at 19:11











  • At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

    – Alexander Mujirishvili
    Nov 25 '18 at 19:15
















0












0








0








I am making simple spring mvc web app and I am using hibernate with oracle. I have two models - User and role and I want to define many to many relationship between them. I have seen several tutorials and have defined my models as shown below:



This is User class:



@Entity
@Table(name = "AppUser")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
@Column(name = "Id")
private int id;

@Column(name = "Username")
private String username;

@Column(name = "Password")
private String password;


@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}
)
@JoinTable
(
name = "user_roles",
joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }
)
private Set<Role> roles = new HashSet<Role>(0);

//getters and setters....
}


This is Role class:



@Entity
@Table(name = "role")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
private int id;

private String role;

@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "roles"
)
private Set<User> users;

//getters and setters....
}


When I run this app on server I get the following error:



enter image description here



This error says that table doesn't exist, so I am interested in should I create it manually(I do not think so) or am I missing anything here?










share|improve this question














I am making simple spring mvc web app and I am using hibernate with oracle. I have two models - User and role and I want to define many to many relationship between them. I have seen several tutorials and have defined my models as shown below:



This is User class:



@Entity
@Table(name = "AppUser")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
@Column(name = "Id")
private int id;

@Column(name = "Username")
private String username;

@Column(name = "Password")
private String password;


@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}
)
@JoinTable
(
name = "user_roles",
joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }
)
private Set<Role> roles = new HashSet<Role>(0);

//getters and setters....
}


This is Role class:



@Entity
@Table(name = "role")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
@SequenceGenerator(name = "id_sequence", sequenceName = "ID_SEQ")
private int id;

private String role;

@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "roles"
)
private Set<User> users;

//getters and setters....
}


When I run this app on server I get the following error:



enter image description here



This error says that table doesn't exist, so I am interested in should I create it manually(I do not think so) or am I missing anything here?







java oracle hibernate spring-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 17:56









Alexander MujirishviliAlexander Mujirishvili

8517




8517













  • what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

    – guleryuz
    Nov 25 '18 at 18:49











  • I have this in my configuration file: hibernate.hbm2ddl.auto=update

    – Alexander Mujirishvili
    Nov 25 '18 at 18:57











  • The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

    – Bob Jarvis
    Nov 25 '18 at 19:11











  • At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

    – Alexander Mujirishvili
    Nov 25 '18 at 19:15





















  • what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

    – guleryuz
    Nov 25 '18 at 18:49











  • I have this in my configuration file: hibernate.hbm2ddl.auto=update

    – Alexander Mujirishvili
    Nov 25 '18 at 18:57











  • The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

    – Bob Jarvis
    Nov 25 '18 at 19:11











  • At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

    – Alexander Mujirishvili
    Nov 25 '18 at 19:15



















what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

– guleryuz
Nov 25 '18 at 18:49





what is the value for property spring.jpa.hibernate.ddl-auto in your configuration files? It should be update.

– guleryuz
Nov 25 '18 at 18:49













I have this in my configuration file: hibernate.hbm2ddl.auto=update

– Alexander Mujirishvili
Nov 25 '18 at 18:57





I have this in my configuration file: hibernate.hbm2ddl.auto=update

– Alexander Mujirishvili
Nov 25 '18 at 18:57













The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

– Bob Jarvis
Nov 25 '18 at 19:11





The error ORA-00942: table or view does not exist occurs when trying to execute an ALTER TABLE on the table user_roles, referencing the table or view AppUser. Perhaps that provides a clue...

– Bob Jarvis
Nov 25 '18 at 19:11













At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

– Alexander Mujirishvili
Nov 25 '18 at 19:15







At this moment my database is empty and I am expecting from hibernate to generate corresponding tables automatically. Which table should be created first?

– Alexander Mujirishvili
Nov 25 '18 at 19:15














1 Answer
1






active

oldest

votes


















2














Just need to put @ManyToMany in one place only.



Please remove @ManyToMany in Role entity.
You can check the link below in order to deal @ManyToMany using oracle database.
I hope this is helpful:



https://gerardnico.com/jpa/many-to-many#oracle_database






share|improve this answer


























  • I have tried it and same happens..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:33











  • Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

    – Jonathan Johx
    Nov 25 '18 at 19:34











  • I removed it but same..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:36











  • Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

    – Jonathan Johx
    Nov 25 '18 at 19:42













  • @AlexanderMujirishvili done

    – Jonathan Johx
    Nov 25 '18 at 19:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470282%2fmanaging-many-to-many-relationship-with-hibernate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Just need to put @ManyToMany in one place only.



Please remove @ManyToMany in Role entity.
You can check the link below in order to deal @ManyToMany using oracle database.
I hope this is helpful:



https://gerardnico.com/jpa/many-to-many#oracle_database






share|improve this answer


























  • I have tried it and same happens..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:33











  • Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

    – Jonathan Johx
    Nov 25 '18 at 19:34











  • I removed it but same..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:36











  • Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

    – Jonathan Johx
    Nov 25 '18 at 19:42













  • @AlexanderMujirishvili done

    – Jonathan Johx
    Nov 25 '18 at 19:47
















2














Just need to put @ManyToMany in one place only.



Please remove @ManyToMany in Role entity.
You can check the link below in order to deal @ManyToMany using oracle database.
I hope this is helpful:



https://gerardnico.com/jpa/many-to-many#oracle_database






share|improve this answer


























  • I have tried it and same happens..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:33











  • Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

    – Jonathan Johx
    Nov 25 '18 at 19:34











  • I removed it but same..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:36











  • Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

    – Jonathan Johx
    Nov 25 '18 at 19:42













  • @AlexanderMujirishvili done

    – Jonathan Johx
    Nov 25 '18 at 19:47














2












2








2







Just need to put @ManyToMany in one place only.



Please remove @ManyToMany in Role entity.
You can check the link below in order to deal @ManyToMany using oracle database.
I hope this is helpful:



https://gerardnico.com/jpa/many-to-many#oracle_database






share|improve this answer















Just need to put @ManyToMany in one place only.



Please remove @ManyToMany in Role entity.
You can check the link below in order to deal @ManyToMany using oracle database.
I hope this is helpful:



https://gerardnico.com/jpa/many-to-many#oracle_database







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 19:49

























answered Nov 25 '18 at 19:31









Jonathan JohxJonathan Johx

1,732317




1,732317













  • I have tried it and same happens..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:33











  • Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

    – Jonathan Johx
    Nov 25 '18 at 19:34











  • I removed it but same..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:36











  • Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

    – Jonathan Johx
    Nov 25 '18 at 19:42













  • @AlexanderMujirishvili done

    – Jonathan Johx
    Nov 25 '18 at 19:47



















  • I have tried it and same happens..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:33











  • Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

    – Jonathan Johx
    Nov 25 '18 at 19:34











  • I removed it but same..

    – Alexander Mujirishvili
    Nov 25 '18 at 19:36











  • Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

    – Jonathan Johx
    Nov 25 '18 at 19:42













  • @AlexanderMujirishvili done

    – Jonathan Johx
    Nov 25 '18 at 19:47

















I have tried it and same happens..

– Alexander Mujirishvili
Nov 25 '18 at 19:33





I have tried it and same happens..

– Alexander Mujirishvili
Nov 25 '18 at 19:33













Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

– Jonathan Johx
Nov 25 '18 at 19:34





Why did you put new HashSet<Role>(0); ? 0? Try remove it ..

– Jonathan Johx
Nov 25 '18 at 19:34













I removed it but same..

– Alexander Mujirishvili
Nov 25 '18 at 19:36





I removed it but same..

– Alexander Mujirishvili
Nov 25 '18 at 19:36













Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

– Jonathan Johx
Nov 25 '18 at 19:42







Oh right!! I noticed that you are working on oracle datanse, let me edit my answer

– Jonathan Johx
Nov 25 '18 at 19:42















@AlexanderMujirishvili done

– Jonathan Johx
Nov 25 '18 at 19:47





@AlexanderMujirishvili done

– Jonathan Johx
Nov 25 '18 at 19:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470282%2fmanaging-many-to-many-relationship-with-hibernate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks