使用SpringData和hibernate创建不成功的表

时间:2016-05-03 12:24:47

标签: hibernate spring-boot

正在创建如下三个实体 -

User.java

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

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="user_id")
      private long id;

      @NotNull
      private String email;

      @NotNull
      private String name;

      @NotNull
      private String contact;

  //getters and setters 

}

Books.java

    @Entity
    @Table(name="books")
    public class Books {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name="isbn")
        private long isbn;

        @NotNull
        private String bookName;

        @NotNull
        private String bookDescription;

        @NotNull
        private String price;

        //getters and setters 
    }

Purchases.java

@Entity
@Table(name="purchases")
public class Purchases {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String order_id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date creation_time;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="user_id")
    private User user;


    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="isbn")
    private Books book;

     // getters and setters   
  }

和控制器

@Controller
public class PurchaseController {

    @Autowired
    UserDAO userDAO;

    @Autowired
    BooksDAO booksDAO;

    @Autowired
    PurchasesDAO purchaseDAO;

    @RequestMapping("/createUser")
    @ResponseBody
    public String createUser(String email, String name, String contact){
        User user=null;
        try{
            user=new User(email,name,contact);
            userDAO.save(user);
            }catch(Exception e){
                return "Problem occured while saving user :"+e; 
            }   
          return "User succesfully created! (id = " + user.getId() + ")";
    }


    @RequestMapping("/createBook")
    @ResponseBody
    public String createBooks(String bookName, String bookDesc, String price){
        Books book=null;
        try{
            book=new Books(bookName,bookDesc,price);
            booksDAO.save(book);
            }catch(Exception e){
                return "Problem occured while saving book details :"+e; 
            }   
          return "Book succesfully created! (isbn = " + book.getIsbn() + " name = "+book.getBookName()+")";
    }

    @RequestMapping("/purchase")
    @ResponseBody
    public String purchase(Long user_id, Long isbn){
        Books book=null;
        User user=null;
        Purchases purchase=null;
        Date creation_date=new Date();
        try{
            book=booksDAO.findOne(isbn);
            user=userDAO.findOne(user_id);
            System.out.println("book= "+book+" and user= "+user+"");
            if(book.getIsbn()==0 || user.getId()==0){
                return "Book with isbn="+isbn+" or user with user id ="+user_id+" not found.";
            }
            purchase = new Purchases(creation_date,user,book);
            purchaseDAO.save(purchase);

        }catch(Exception e){
            return "Exception occured while purchasing book ["+e+"]";
        }

        return "Book purchased";
    }}

dao接口扩展CrudRepository<Class, Long>{},注释为transactional。问题是第三个表是purchase.java没有被创建。在控制台上获取以下内容。

2016-05-03 18:13:33.876  INFO 19570 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2016-05-03 18:13:33.920  INFO 19570 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.8.Final}
2016-05-03 18:13:33.921  INFO 19570 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-05-03 18:13:33.922  INFO 19570 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-05-03 18:13:34.062  INFO 19570 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2016-05-03 18:13:34.388  INFO 19570 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2016-05-03 18:13:34.465  INFO 19570 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-05-03 18:13:34.707  INFO 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: alter table purchases drop foreign key FK_laat6sjwiu2hcenno94vyj0jb
2016-05-03 18:13:34.716 ERROR 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table purchases drop foreign key FK_laat6sjwiu2hcenno94vyj0jb
2016-05-03 18:13:34.716 ERROR 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'purchase_books.purchases' doesn't exist
Hibernate: alter table purchases drop foreign key FK_klpjcob070n95mdvt1473hs1s
2016-05-03 18:13:34.716 ERROR 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table purchases drop foreign key FK_klpjcob070n95mdvt1473hs1s
2016-05-03 18:13:34.716 ERROR 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'purchase_books.purchases' doesn't exist
Hibernate: drop table if exists books
Hibernate: drop table if exists purchases
Hibernate: drop table if exists user_details
Hibernate: create table books (isbn bigint not null auto_increment, book_description varchar(255) not null, book_name varchar(255) not null, price varchar(255) not null, primary key (isbn))
Hibernate: create table purchases (order_id bigint not null auto_increment, creation_time datetime, isbn bigint, user_id bigint, primary key (order_id))
Hibernate: create table user_details (user_id bigint not null auto_increment, contact varchar(255) not null, email varchar(255) not null, name varchar(255) not null, primary key (user_id))
Hibernate: alter table purchases add constraint FK_laat6sjwiu2hcenno94vyj0jb foreign key (isbn) references books (isbn)
Hibernate: alter table purchases add constraint FK_klpjcob070n95mdvt1473hs1s foreign key (user_id) references user_details (user_id)
2016-05-03 18:13:37.037  INFO 19570 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

使用SpringBoot运行应用程序。在消息中提到了问题,但无法弄清楚。任何人都可以详细说明或告诉如何解决这个问题?表是在sql数据库中使用hibernate创建的。

2 个答案:

答案 0 :(得分:1)

检查“购买”order_id上的类型。我想你可能希望它是一个数字,也许是类型&#34; long&#34;。

答案 1 :(得分:0)

无法将@GeneratedValue(strategy = GenerationType.AUTO)用于String属性

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String order_id;

将其更改为Long,例如

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long order_id;

请不要使用user等表名。它是PostgreSQL中的保留关键字。