我尝试在 AWS 和 Openshift 上部署我的应用程序,这些应用程序似乎工作正常,直到我尝试/users
在 POST 之后访问 GET 端点(保存的用户没有公司数据)。
它返回:
出现意外错误(类型=内部服务器错误,状态=500)。 无法准备陈述;SQL [选择company0_.users_id 作为users_id1_8_0_,company0_.companies_id 作为companie2_8_0_,company1_.id 作为id1_0_1_,company1_.contact_id 作为contact_4_0_1_,company1_.gallery_id 作为gallery_5_0_1_,company1_.active 作为active2_0_1_,company1_.promoted 作为promoted3_0_1_id1 company1_6. ,company1_.profile_id 作为 profile_7_0_1_,company1_.subscription_id 作为 subscrip8_0_1_,contact2_.id 作为 id1_1_2_,contact2_.email 作为 email2_1_2_,contact2_.phone 作为 phone3_1_2_,gallery3_.id 作为 id1_2_3_,location4_.id 作为 id1_3_4_,location4_4_address 作为 address2_3_4_, .city 作为city3_3_4_,location4_.country 作为country4_3_4_,location4_.country_code 作为country_5_3_4_,location4_.latitude 作为latitude6_3_4_,location4_.longitude 作为longitud7_3_4_,profile5_.id 作为id1_5_5_,profile5_。
日志如下:
org.springframework.dao.InvalidDataAccessResourceUsageException:无法准备语句
引起:java.sql.SQLSyntaxErrorException:用户缺少权限或找不到对象:语句中的COMPANIES
引起:org.hsqldb.HsqlException:用户缺少权限或找不到对象:COMPANIES
但是所有这些都可以在本地服务器上使用 mysql 正常工作。
控制器:
@RestController
public class UserController {
@Autowired
UserRepository repository;
@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getUsers() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
}
存储库:
@Repository
public interface UserRepository extends JpaRepository<User, String> {
}
用户型号:
@Entity
@Table(name = "users")
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false)
@SerializedName("id")
private long id;
@Column(name = "name")
@SerializedName("name")
private String name;
@Column(name = "email", unique = true, updatable = false)
@SerializedName("email")
private String email;
@Column(name = "password")
@SerializedName("password")
@JsonIgnore
private String password;
@Column(name = "accessToken")
@SerializedName("accessToken")
private String accessToken;
@Column(name = "phoneNumber")
@SerializedName("phoneNumber")
private String phoneNumber;
@Column(name = "createdAt", nullable = false, updatable = false)
@SerializedName("createdAt")
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(name = "updatedAt", nullable = false)
@SerializedName("updatedAt")
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Company> companies;
//getters & setters etc
}
公司型号:
@Entity
@Table(name = "companies")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false)
@SerializedName("id")
private long id;
@OneToOne(cascade = CascadeType.ALL)
@SerializedName("profile")
private Profile profile;
@OneToOne(cascade = CascadeType.ALL)
@SerializedName("contact")
private Contact contact;
@OneToOne(cascade = CascadeType.ALL)
@SerializedName("location")
private Location location;
@OneToOne(cascade = CascadeType.ALL)
@SerializedName("gallery")
private Gallery gallery;
@OneToOne(cascade = CascadeType.ALL)
@SerializedName("subscription")
private Subscription subscription;
@Column(name = "active", columnDefinition = "tinyint(1) default 0")
@SerializedName("active")
private boolean isActive;
@Column(name = "promoted", columnDefinition = "tinyint(1) default 0")
@SerializedName("promoted")
private boolean isPromoted;
//getters & setters etc
}
应用属性:
spring.datasource.url=jdbc:mysql://host:3306/database
spring.datasource.username=user
spring.datasource.password=password
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
知道是什么导致了这个问题吗?我在上面也提到这在本地机器上工作得很好。
更新如果我在将用户(exp.User/Company、Company/Profile、Company/Contact 等)保存到数据库时不为关系中的每个对象提供数据,它似乎会给出此错误。