Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "users")
- public class User implements Serializable {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- @Column(name = "`name`", nullable = false, unique = true)
- private String name;
- @OneToMany(fetch = FetchType.EAGER, mappedBy = "author")
- private Set<Document> authorDocuments = new HashSet<>();
- @OneToMany(fetch = FetchType.EAGER, mappedBy = "concurrent")
- private Set<Document> concurrentDocuments = new HashSet<>();
- public User() {
- }
- public User(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Document> getAuthorDocuments() {
- return authorDocuments;
- }
- public Set<Document> getConcurrentDocuments() {
- return concurrentDocuments;
- }
- public void addDocumentAuthor(Document document) {
- authorDocuments.add(document);
- document.setAuthor(this);
- }
- public void removeDocumentAuthor(Document document) {
- authorDocuments.remove(document);
- document.setAuthor(null);
- }
- public void addDocumentConcurrent(Document document) {
- concurrentDocuments.add(document);
- document.setConcurrent(this);
- }
- public void removeDocumentConcurrent(Document document) {
- concurrentDocuments.remove(document);
- document.setConcurrent(null);
- }
- }
- @Entity
- @Table(name = "documents")
- public class Document implements Serializable {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- @Column(name = "`name`", nullable = false, unique = true)
- private String name;
- @ManyToOne(fetch = FetchType.EAGER)
- @JoinColumn(name = "id_author", nullable = false)
- private User author;
- @ManyToOne(fetch = FetchType.EAGER)
- @JoinColumn(name = "id_concurrent", nullable = false)
- private User concurrent;
- public Document() {
- }
- public Document(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public User getAuthor() {
- return author;
- }
- public void setAuthor(User author) {
- this.author = author;
- }
- public User getConcurrent() {
- return concurrent;
- }
- public void setConcurrent(User concurrent) {
- this.concurrent = concurrent;
- }
- @Override
- public String toString() {
- return "Document {" + lineSeparator()
- + "name: " + name + "," + lineSeparator()
- + "author: " + author.getName() + "," + lineSeparator()
- + "concurrent: " + concurrent.getName() + lineSeparator()
- + "}";
- }
- }
- @Repository
- public interface DocumentRepository extends CrudRepository<Document, Integer> {
- }
- @Repository
- public interface UserRepository extends CrudRepository<User, Integer> {
- }
- spring.jpa.hibernate.ddl-auto=validate
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- spring.jpa.show-sql=true
- spring.datasource.url=jdbc:mysql://localhost:3306/many_to_many?useSSL=false
- spring.datasource.username=root
- spring.datasource.password=root
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- drop database if exists many_to_many;
- create database many_to_many;
- use many_to_many;
- create table users (
- id int primary key auto_increment,
- `name` varchar(255) not null unique
- );
- create table documents (
- id int primary key auto_increment,
- `name` varchar(255) not null unique,
- id_author int not null,
- id_concurrent int not null,
- foreign key(id_author) references users(id),
- foreign key(id_concurrent) references users(id)
- );
- @SpringBootApplication
- public class Application implements CommandLineRunner {
- @Autowired
- private UserRepository userRepository;
- @Autowired
- private DocumentRepository documentRepository;
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- @Override
- public void run(String... args) throws Exception {
- User user1 = new User("user1");
- User user2 = new User("user2");
- User user3 = new User("user3");
- userRepository.save(user1);
- userRepository.save(user2);
- userRepository.save(user3);
- Document document1 = new Document("document1");
- Document document2 = new Document("document2");
- user1.addDocumentAuthor(document1);
- user2.addDocumentConcurrent(document1);
- user1.addDocumentAuthor(document2);
- user3.addDocumentConcurrent(document2);
- documentRepository.save(document1);
- documentRepository.save(document2);
- documentRepository.findAll().forEach(System.out::println);
- }
- }
- Document {
- name: document1,
- author: user1,
- concurrent: user2,
- }
- Document {
- name: document2,
- author: user1,
- concurrent: user3,
- }
Add Comment
Please, Sign In to add comment