H2-控制台未显示插入的数据?

时间:2019-01-19 10:26:24

标签: java spring-boot intellij-idea h2

我想使用Spring-boot向h2-console添加数据...但是当我     运行代码,没有任何内容插入到h2-console中,也没有引发任何错误。

这是我的Author类,具有姓和名作为变量。

package guru.springframework.spring5webapp;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/* 18/01/19
    POJO class of Author
 */
@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO )
    private long id;
    private String firstname;
    private String lastname;

    @ManyToMany(mappedBy = "authors")           //???mapped by?
    private Set<Book> books = new HashSet<>();

    public Author(){}

    public Author(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public Author(String firstname, String lastname, Set<Book> books) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.books = books;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Set<Book> getBooks() {
        return books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Author author = (Author) o;
        return id == author.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Author{" +
                "id=" + id +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", books=" + books +
                '}';
    }
}
  

这是我的书籍类,具有id,title,isbn和Publisher作为变量。

package guru.springframework.spring5webapp;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/* 18/01/19
POJO class of Book
*/
@Entity                                 //mark as JPA entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO )
private long id;
private String title;
private String isbn;
private String publisher;

@ManyToMany
@JoinTable(name = "author_book" , joinColumns = 
@JoinColumn(name="book_id"),inverseJoinColumns = @JoinColumn(name = 
"author_id"))  //to join only one table???
private Set<Author> authors = new HashSet<>();

public Book(){}

public Book(String title, String isbn, String publisher, Set<Author> 
authors) {
    this.title = title;
    this.isbn = isbn;
    this.publisher = publisher;
    this.authors = authors;
}

public Book(String title, String isbn, String publisher) {
    this.title = title;
    this.isbn = isbn;
    this.publisher = publisher;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public String getPublisher() {
    return publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public Set<Author> getAuthors() {
    return authors;
}

public void setAuthors(Set<Author> authors) {
    this.authors = authors;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Book book = (Book) o;
    return id == book.id;
}

@Override
public int hashCode() {
    return Objects.hash(id);
}

@Override
public String toString() {
    return "Book{" +
            "id=" + id +
            ", title='" + title + '\'' +
            ", isbn='" + isbn + '\'' +
            ", publisher='" + publisher + '\'' +
            ", authors=" + authors +
            '}';
    }
}
  

两个接口的作者和书,即Spring Data JPA存储库。

package repositories;
//AuthorRepository.java
import guru.springframework.spring5webapp.Author;
import org.springframework.data.repository.CrudRepository;

public interface AuthorRepository extends CrudRepository<Author,Long> {
}

package repositories;
//BookRepository.java
import guru.springframework.spring5webapp.Book;
import org.springframework.data.repository.CrudRepository;

public interface BookRepository extends CrudRepository<Book,Long> {
}

这是DevBootstrap.java类(插入到h2-console中的数据) 当我在DevBootstrap.java之后运行时,Spring正常启动,没有错误,也没有其他错误产生。.

package bootstrap;

import guru.springframework.spring5webapp.Author;
import guru.springframework.spring5webapp.Book;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import repositories.AuthorRepository;
import repositories.BookRepository;

//This is Run time class...this class is managed by Spring framework during 
runtime

//2)Implement ApplicationListener<ContectRefreshedEvent>
@Component
public class DevBootstrap implements 
ApplicationListener<ContextRefreshedEvent> 
{

//4)Create object of interface Author and Book repositorhy
private AuthorRepository authorRepository;
private BookRepository bookRepository;

//5)Constructor Auto Injection
public DevBootstrap(AuthorRepository authorRepository, BookRepository 
bookRepository) {
    this.authorRepository = authorRepository;
    this.bookRepository = bookRepository;
}
//3)Create onApplicationEvent method and call init()
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) 
    {

     initData();
    }
//1)
    private void initData(){

        //Eric
        Author eric = new Author("Eric","Evans");
        Book ddd= new Book("Domain Driven Design","1234","Harper Collins");
        eric.getBooks().add(ddd);
        ddd.getAuthors().add(eric);

        //6)
        authorRepository.save(eric);
        bookRepository.save(ddd);

        //Rod
        Author rod = new Author("Rod","Johnson");
        Book noEJB = new Book("J2EE Developement without 
        EJB","23444","Worx");
        rod.getBooks().add(noEJB);

        //7)
        authorRepository.save(rod);
        bookRepository.save(noEJB);
    }
}

0 个答案:

没有答案