2017-02-27 225 views
1

當我使用Hibernate和postgresql啓動Spring Boot應用程序時,我遇到了10個錯誤。我該如何解決它們?有什麼問題? 這裏是我的實體:Hibernate和postgresql。導出架構時出錯

Customer.java

@Entity 
@Access(AccessType.FIELD) // so I can avoid using setters for fields that won't change 
public class Customer { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long customerId; 

    @Embedded 
    private FirstName firstName; 
    @Embedded 
    private LastName lastName; 
    @Embedded 
    private PhoneNumber phoneNumber; 

    @ManyToOne 
    @JoinColumn(name = "ADDRESS_ID") 
    private Address address; 

    @OneToOne 
    @JoinColumn(name = "USER_ID") 
    private User user; 

    // jpa requirement 
    public Customer() { 
    } 

    public Customer(FirstName firstName, LastName lastName, 
     PhoneNumber phoneNumber, Address address, User user) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.phoneNumber = phoneNumber; 
    this.address = address; 
    this.user = user; 
    } 

    public Long getCustomerId() { 
    return customerId; 
    } 

    public FirstName getFirstName() { 
    return firstName; 
    } 

    public LastName getLastName() { 
    return lastName; 
    } 

    public PhoneNumber getPhoneNumber() { 
    return phoneNumber; 
    } 

    // setter for phone number is needed because customer can change his phone number 
    public void setPhoneNumber(PhoneNumber phoneNumber) { 
    this.phoneNumber = phoneNumber; 
    } 

    public Address getAddress() { 
    return address; 
    } 

    // setter for address is needed because customer can change his address 
    public void setAddress(Address address) { 
    this.address = address; 
    } 

    public User getUser() { 
    return user; 
    } 

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

    Customer customer = (Customer) o; 

    if (firstName != null ? !firstName.equals(customer.firstName) : customer.firstName != null) { 
     return false; 
    } 
    if (lastName != null ? !lastName.equals(customer.lastName) : customer.lastName != null) { 
     return false; 
    } 
    return user != null ? user.equals(customer.user) : customer.user == null; 
    } 

    @Override 
    public int hashCode() { 
    int result = firstName != null ? firstName.hashCode() : 0; 
    result = 31 * result + (lastName != null ? lastName.hashCode() : 0); 
    result = 31 * result + (user != null ? user.hashCode() : 0); 
    return result; 
    } 

    @Override 
    public String toString() { 
    return "Customer{" + 
     "firstName=" + firstName + 
     ", lastName=" + lastName + 
     ", phoneNumber=" + phoneNumber + 
     '}'; 
    } 
} 

User.java

@Entity 
public class User { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long userId; 

    @Embedded 
    private EmailAddress emailAddress; 
    @Embedded 
    private Password password; 

    @OneToOne 
    @JoinColumn(name = "CUSTOMER_ID") 
    private Customer customer; 

    @Column 
    @Enumerated(EnumType.STRING) 
    private UserRole userRole; 

    // jpa requirement 
    public User() { 
    } 

    public User(EmailAddress emailAddress, Password password) { 
    this.emailAddress = emailAddress; 
    this.password = password; 
    this.userRole = UserRole.USER; // on creation everyone is just a user 
    } 

    public Long getUserId() { 
    return userId; 
    } 

    public EmailAddress getEmailAddress() { 
    return emailAddress; 
    } 

    public void setEmailAddress(EmailAddress emailAddress) { 
    this.emailAddress = emailAddress; 
    } 

    public Password getPassword() { 
    return password; 
    } 

    public void setPassword(Password password) { 
    this.password = password; 
    } 

    public Customer getCustomer() { 
    return customer; 
    } 

    public void setCustomer(Customer customer) { 
    this.customer = customer; 
    } 

    public UserRole getUserRole() { 
    return userRole; 
    } 

    public void setUserRole(UserRole userRole) { 
    this.userRole = userRole; 
    } 
} 

而且Book.java

@Entity 
@Access(AccessType.FIELD) // so I can avoid using setters for fields that won't change 
public class Book { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long bookId; 

    @Embedded 
    private Isbn isbn; 
    @Embedded 
    private Title title; 
    @Embedded 
    private Author author; 
    @Embedded 
    private Genre genre; 
    private Year publicationYear; 
    private BigDecimal price; 

    // jpa requirement 
    public Book() { 
    } 

    public Book(Isbn isbn, Title title, Author author, Genre genre, Year publicationYear, 
     BigDecimal price) { 
    this.isbn = isbn; 
    this.title = title; 
    this.author = author; 
    this.genre = genre; 
    this.publicationYear = publicationYear; 
    this.price = price; 
    } 

    public Long getBookId() { 
    return bookId; 
    } 

    public Isbn getIsbn() { 
    return isbn; 
    } 

    public Title getTitle() { 
    return title; 
    } 

    public Author getAuthor() { 
    return author; 
    } 

    public Genre getGenre() { 
    return genre; 
    } 

    public BigDecimal getPrice() { 
    return price; 
    } 

    public Year getPublicationYear() { 
    return publicationYear; 
    } 

    // setter for price is needed because price of the book can change (discounts and so on) 
    public void setPrice(BigDecimal price) { 
    this.price = price; 
    } 

} 

我對PostgreSQL設置application.properties:

spring.datasource.url= jdbc:postgresql://localhost:5432/bookrest 
spring.datasource.username=postgres 
spring.datasource.password=password 

spring.jpa.hibernate.ddl-auto=create-drop 

這裏是休眠部分堆棧跟蹤(ommitted所有controoller映射):

2017-02-27 11:03:05.481 INFO 6563 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ 
    name: default 
    ...] 
2017-02-27 11:03:05.555 INFO 6563 --- [ restartedMain] org.hibernate.Version     : HHH000412: Hibernate Core {5.0.11.Final} 
2017-02-27 11:03:05.556 INFO 6563 --- [ restartedMain] org.hibernate.cfg.Environment   : HHH000206: hibernate.properties not found 
2017-02-27 11:03:05.558 INFO 6563 --- [ restartedMain] org.hibernate.cfg.Environment   : HHH000021: Bytecode provider name : javassist 
2017-02-27 11:03:05.609 INFO 6563 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
2017-02-27 11:03:05.819 INFO 6563 --- [ restartedMain] org.hibernate.dialect.Dialect   : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect 
2017-02-27 11:03:05.997 INFO 6563 --- [ restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl  : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 
2017-02-27 11:03:05.999 INFO 6563 --- [ restartedMain] org.hibernate.type.BasicTypeRegistry  : HHH000270: Type registration [java.util.UUID] overrides previous : [email protected] 
2017-02-27 11:03:06.228 WARN 6563 --- [ restartedMain] org.hibernate.orm.deprecation   : HHH90000014: Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead. See Hibernate Domain Model Mapping Guide for details. 
2017-02-27 11:03:06.580 INFO 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export 
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKglkhkmh2vyn790ijs6hiqqpi 
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist 
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKidmyb2vdwmk3o502u0rg8g32h 
2017-02-27 11:03:06.585 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist 
2017-02-27 11:03:06.585 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user drop constraint FK2q989f4c89rv2b9xvtomfc0fs 
2017-02-27 11:03:06.586 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user" 
    Position: 13 
2017-02-27 11:03:06.587 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop table if exists user cascade 
2017-02-27 11:03:06.587 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user" 
    Position: 22 
2017-02-27 11:03:06.588 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop sequence hibernate_sequence 
2017-02-27 11:03:06.588 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: sequence "hibernate_sequence" does not exist 
2017-02-27 11:03:06.607 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table user (user_id bigserial not null, email_address varchar(255), password varchar(255), user_role varchar(255), customer_customer_id int8, primary key (user_id)) 
2017-02-27 11:03:06.607 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user" 
    Position: 14 
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer add constraint FKidmyb2vdwmk3o502u0rg8g32h foreign key (user_user_id) references user 
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user" 
    Position: 103 
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user add constraint FK2q989f4c89rv2b9xvtomfc0fs foreign key (customer_customer_id) references customer 
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user" 
    Position: 13 
2017-02-27 11:03:06.611 INFO 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete 
2017-02-27 11:03:06.673 INFO 6563 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 

編輯1:

改變後我的用戶到AppUser(表名用戶被禁止)我仍然收到幾個錯誤:

2017-02-27 11:45:44.253 INFO 9560 --- [ restartedMain] org.hibernate.Version     : HHH000412: Hibernate Core {5.0.11.Final} 
2017-02-27 11:45:44.255 INFO 9560 --- [ restartedMain] org.hibernate.cfg.Environment   : HHH000206: hibernate.properties not found 
2017-02-27 11:45:44.256 INFO 9560 --- [ restartedMain] org.hibernate.cfg.Environment   : HHH000021: Bytecode provider name : javassist 
2017-02-27 11:45:44.298 INFO 9560 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
2017-02-27 11:45:44.621 INFO 9560 --- [ restartedMain] org.hibernate.dialect.Dialect   : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect 
2017-02-27 11:45:44.825 INFO 9560 --- [ restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl  : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 
2017-02-27 11:45:44.828 INFO 9560 --- [ restartedMain] org.hibernate.type.BasicTypeRegistry  : HHH000270: Type registration [java.util.UUID] overrides previous : [email protected] 
2017-02-27 11:45:45.370 INFO 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export 
2017-02-27 11:45:45.373 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table app_user drop constraint FKf8cjd2mkc4tu1u5nhju0clae7 
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "app_user" does not exist 
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKglkhkmh2vyn790ijs6hiqqpi 
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist 
2017-02-27 11:45:45.375 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKslkyb5dphxe4c7au3hqx3la6m 
2017-02-27 11:45:45.375 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist 
2017-02-27 11:45:45.408 INFO 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete 
2017-02-27 11:45:45.467 INFO 9560 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 
+0

可能重複:http://stackoverflow.com/questions/22959373/hibernate-奇怪的例外 – esin88

回答

3

user是PostgreSQL中reserved keyword。我建議重新命名實體類的一些其它的名字,或者只是使用hibernate annotations@Table指定的PostgreSQL友好的表名,如下所示:

@Entity 
@Table(name = "library_user") 
public class User {...} 
+0

改變了,但我仍然收到一些錯誤 - >在編輯1中發佈它們。 – doublemc

+0

@doublemc這可能是由你的'create-drop'策略引起的。 Hibernate試圖刪除舊錶,並且找不到這個新的'app_user'。我建議手動刪除所有表格和約束條件,然後重新運行你的應用程序。 – esin88

+0

問題是我沒有用戶實體中的ID設置,謝謝你的幫助。 – doublemc

0

用戶在大多數(如果不是全部)數據庫中都是保留字。爲此目的使用其他名稱。

Words