Oracle spring启动休眠查询无效用户错误
嗨,我是春天靴子的新手。我尝试连接到Oracle并列出相关记录。我的代码在存根环境中工作,也就是说没有连接到数据库。当我试图从Spring连接到db时,我得到了编辑2中给出的错误: HomeControllerOracle spring启动休眠查询无效用户错误,oracle,spring-boot,hibernate,jpa,spring-data-jpa,Oracle,Spring Boot,Hibernate,Jpa,Spring Data Jpa,嗨,我是春天靴子的新手。我尝试连接到Oracle并列出相关记录。我的代码在存根环境中工作,也就是说没有连接到数据库。当我试图从Spring连接到db时,我得到了编辑2中给出的错误: HomeController package blog.controllers; import blog.models.Post; import blog.services.PostService; import org.springframework.beans.factory.annotation.Autowi
package blog.controllers;
import blog.models.Post;
import blog.services.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.stream.Collectors;
@Controller
public class HomeController {
@Autowired
private PostService postService;
@RequestMapping("/")
public String index(Model model){
List<Post> latest5Posts = postService.findLatest5();
model.addAttribute("latest5posts", latest5Posts);
List<Post> latest3Posts = latest5Posts.stream()
.limit(3).collect(Collectors.toList());
model.addAttribute("latest3posts", latest3Posts);
return "index";
}
}
用户实体类
package blog.models;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "POSTS")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 300)
private String title;
@Lob @Column(nullable = false)
private String body;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private User author;
@Column(nullable = false)
private Date creation_date = new Date();
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 getBody() {return body;}
public void setBody(String body) {this.body = body;}
public User getAuthor() {return author;}
public void setAuthor(User author) {this.author = author;}
public Date getCreation_date() {return date;}
public void setCreation_date(Date creation_date) {this.creation_date = creation_date;}
public Post() { }
public Post(Long id, String title, String body, User author) {
this.id=id; this.title=title; this.body=body; this.author=author;
}
@Override
public String toString() {
return "Post{" + "id=" + id + ", title='" + title + '\'' +
", body='" + body + '\'' +
", author='" + author + '\'' +
", date=" + Creationdate +
'}';
}
}
package blog.models;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 30, unique = true)
private String username;
@Column(length = 60)
private String passwordHash;
@Column(length = 100)
private String fullName;
@OneToMany(mappedBy = "author")
private Set<Post> posts = new HashSet<>();
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
public String getPasswordHash() {return passwordHash;}
public void setPasswordHash(String passwordHash) {this.passwordHash = passwordHash;}
public String getFullName() {return fullName;}
public void setFullName(String fullName) {this.fullName = fullName;}
public Set<Post> getPosts() {return posts;}
public void setPosts(Set<Post> posts) {this.posts = posts;}
public User() { }
public User(Long id, String username, String fullName) {
this.id = id; this.username = username; this.fullName = fullName;
}
@Override
public String toString() {
return "User{" + "id=" + id + ", username='" + username + '\'' +
", passwordHash='" + passwordHash + '\'' +
", fullName='" + fullName + '\'' + '}';
}
}
#Turn off Thymeleaf cache
spring.thymeleaf.cache = false
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@...
spring.datasource.username = usrnm
spring.datasource.password = psswrd
# Configure Hibernate DDL mode: create / update
spring.jpa.properties.hibernate.hbm2ddl.auto = update
#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.data.jpa.repositories.enabled=true
# Show or not log for each sql query
spring.jpa.show-sql = true
p、 我还通过更改存储库尝试了以下方法:
@Query(value = "SELECT * FROM POSTS aa LEFT JOIN USERS bb ON aa.AUTHOR_ID=bb.ID", nativeQuery = true)
这次我出现了以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at ...
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:386) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:214) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:470) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
... 27 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: ORA-02268: referenced table does not have a primary key
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1814) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1779) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:277) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
... 37 common frames omitted
用户存储库
package blog.repositories;
import blog.models.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;
import java.util.List;
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.creation_date DESC")
List<Post> findLatest5Posts(Pageable pageable);
}
package blog.repositories;
import blog.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
编辑2:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at ...
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:386) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:214) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:470) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
... 27 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: ORA-02268: referenced table does not have a primary key
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1814) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1779) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:277) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
... 37 common frames omitted
JPA查询的错误行(未使用本机查询时)
问题似乎如下: 由于这一特性:
spring.jpa.properties.hibernate.hbm2ddl.auto = update
由于Posts实体已更改为引用用户,Hibernate尝试添加外键约束:
alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users
但是得到这个错误:
ORA-02268: referenced table does not have a primary key
不确定为什么hibernate不向该表添加主键,因为@Id注释显然在那里
尝试手动向Users.id列添加主键约束:
ALTER TABLE users
ADD CONSTRAINT users_pk PRIMARY KEY (id);
更新
package blog.services;
import blog.models.Post;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface PostService {
List<Post> findAll();
List<Post> findLatest5();
Post findById(Long id);
Post create(Post post);
Post edit(Post post);
void deleteById(Long id);
}
package blog.services;
import blog.models.User;
import java.util.List;
public interface UserService {
List<User> findAll();
User findById(Long id);
User create(User user);
User edit(User user);
void deleteById(Long id);
}
在order by子句中使用Post.data映射时,可能会出现问题
医生说:
必须为持久字段或属性指定此批注
类型为java.util.Date和java.util.Calendar。可能只是
为这些类型的字段或属性指定
在使用java.util.Date时,需要添加以下内容:
@Temporal(TemporalType.DATE)
private Date date = new Date();
问题似乎如下: 由于这一特性:
spring.jpa.properties.hibernate.hbm2ddl.auto = update
由于Posts实体已更改为引用用户,Hibernate尝试添加外键约束:
alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users
但是得到这个错误:
ORA-02268: referenced table does not have a primary key
不确定为什么hibernate不向该表添加主键,因为@Id注释显然在那里
尝试手动向Users.id列添加主键约束:
ALTER TABLE users
ADD CONSTRAINT users_pk PRIMARY KEY (id);
更新
package blog.services;
import blog.models.Post;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface PostService {
List<Post> findAll();
List<Post> findLatest5();
Post findById(Long id);
Post create(Post post);
Post edit(Post post);
void deleteById(Long id);
}
package blog.services;
import blog.models.User;
import java.util.List;
public interface UserService {
List<User> findAll();
User findById(Long id);
User create(User user);
User edit(User user);
void deleteById(Long id);
}
在order by子句中使用Post.data映射时,可能会出现问题
医生说:
必须为持久字段或属性指定此批注
类型为java.util.Date和java.util.Calendar。可能只是
为这些类型的字段或属性指定
在使用java.util.Date时,需要添加以下内容:
@Temporal(TemporalType.DATE)
private Date date = new Date();
请张贴完整的stacktrace。。您还有任何spring配置文件吗?java还是xml?谢谢您的回复@MaciejKowalski!请查看编辑部分。确定您可以添加@SpringBootApplication公共类应用程序{}实现吗?是的,我刚刚添加了它。好的。。最后,您可以添加org.springframework.beans.factory.UnsatisfiedDependencyException的完整跟踪:错误创建。。例外情况请发布完整的stacktrace。。您还有任何spring配置文件吗?java还是xml?谢谢您的回复@MaciejKowalski!请查看编辑部分。确定您可以添加@SpringBootApplication公共类应用程序{}实现吗?是的,我刚刚添加了它。好的。。最后,您可以添加org.springframework.beans.factory.UnsatisfiedDependencyException的完整跟踪:错误创建。。例外情况我已经相应地手动创建了主键。现在,当我运行这个应用程序时,它被困在运行hbm2ddl模式更新的行中。当使用本机查询时,似乎也不允许使用Pageable类。当我再次尝试查询(@query(“SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC”))时,我又出现了一些错误。要查看它们,请参见上面的编辑2。我已经手动创建了相应的主键。现在,当我运行这个应用程序时,它被困在运行hbm2ddl模式更新的行中。当使用本机查询时,似乎也不允许使用Pageable类。当我再次尝试查询(@query(“SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC”))时,我又出现了一些错误。要查看它们,请参见上面的编辑2。