我的Hibernate尝试持久化并从数据库中查找实例时遇到问题。在调查之后,建议删除表并重新运行该应用程序可能会有所帮助。在数据库中执行命令drop table users;
之后,我的spring应用程序将不会在架构上重新创建实体。我曾尝试更改dll-auto
属性,但没有任何反应。我正在使用spring-boot-starter-data-jpa
依赖关系,而不是hibernate.cfg.xml文件。此外,在将用户持久保存到数据库后(如果有用户表),尽管存在用户行,但EntityManager.find()方法仍返回null。
application.properties:
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://ec2-54-243-187-30.compute-1.amazonaws.com:5432/dbte05lnlcfhd2?user=gmjgnsqxmifijr&password=cc713437b57783d84638d0a3a91bcecd62527cbef98c7b4c25188113dc8c5147&sslmode=require
spring.datasource.username=
spring.datasource.password=
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: false
User.java:
package ca.mcgill.ecse321.ridesharing.model;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT e FROM User e")
})
public class User{
private List<Double> ratings;
public void setRatings(List<Double> ratings) {
this.ratings=ratings;
}
public void addRating(double rating) {
this.ratings.add(rating);
}
@ElementCollection
@CollectionTable(name="ratings", joinColumns=@JoinColumn(name="username"))
@Column(name="rating")
public List<Double> getRatings(){
return this.ratings;
}
private String username;
public void setUsername(String value) {
this.username = value;
}
@Id
@Column(name = "username")
public String getUsername() {
return this.username;
}
private String password;
public void setPassword(String value) {
this.password = value;
}
@Column
public String getPassword() {
return this.password;
}
private String firstName;
public void setFirstName(String value) {
this.firstName = value;
}
@Column
public String getFirstName() {
return this.firstName;
}
private String lastName;
public void setLastName(String value) {
this.lastName = value;
}
@Column
public String getLastName() {
return this.lastName;
}
private String phoneNumber;
public void setPhoneNumber(String value) {
this.phoneNumber = value;
}
@Column
public String getPhoneNumber() {
return this.phoneNumber;
}
private String city;
public void setCity(String value) {
this.city = value;
}
@Column
public String getCity() {
return this.city;
}
private String address;
public void setAddress(String value) {
this.address = value;
}
@Column
public String getAddress() {
return this.address;
}
private double avgRating;
public void setAvgRating(double value) {
this.avgRating = value;
}
@Column
public double getAvgRating() {
return this.avgRating;
}
private int numTrips;
public void setNumTrips(int value) {
this.numTrips = value;
}
public void increaseNumTrips() {
this.numTrips = numTrips + 1;
}
@Column
public int getNumTrips() {
return this.numTrips;
}
private String role;
public void setRole(String value) {
this.role = value;
}
@Column
public String getRole() {
return this.role;
}
private Car car;
@OneToOne(mappedBy="driver", optional=true)
public Car getCar() {
return this.car;
}
public void setCar(Car car) {
this.car = car;
}
private Location request;
@OneToOne(mappedBy="passenger", optional=false)
public Location getRequest() {
return this.request;
}
public void setRequest(Location request) {
this.request = request;
}
}
启动时启动控制台日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.6.RELEASE)
2018-11-01 11:22:11.577 INFO 76995 --- [ main] c.m.e.r.RideSharingApplication : Starting RideSharingApplication on Noams-MacBook-Pro.local with PID 76995 (/Users/noamsuissa/Desktop/RideSharing/RideSharingv2/target/classes started by noamsuissa in /Users/noamsuissa/Desktop/RideSharing/RideSharingv2)
2018-11-01 11:22:11.581 INFO 76995 --- [ main] c.m.e.r.RideSharingApplication : No active profile set, falling back to default profiles: default
2018-11-01 11:22:11.647 INFO 76995 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@14a2f921: startup date [Thu Nov 01 11:22:11 EDT 2018]; root of context hierarchy
2018-11-01 11:22:12.593 INFO 76995 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f345ba74] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-01 11:22:12.931 INFO 76995 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-11-01 11:22:12.949 INFO 76995 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-11-01 11:22:12.950 INFO 76995 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-11-01 11:22:12.953 INFO 76995 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/noamsuissa/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-11-01 11:22:13.027 INFO 76995 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-11-01 11:22:13.028 INFO 76995 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1386 ms
2018-11-01 11:22:13.091 INFO 76995 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-11-01 11:22:13.096 INFO 76995 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-01 11:22:13.096 INFO 76995 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-01 11:22:13.096 INFO 76995 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-11-01 11:22:13.096 INFO 76995 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-01 11:22:13.236 INFO 76995 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-11-01 11:22:13.736 INFO 76995 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-11-01 11:22:13.803 INFO 76995 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-11-01 11:22:13.820 INFO 76995 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2018-11-01 11:22:13.994 INFO 76995 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.17.Final}
2018-11-01 11:22:13.995 INFO 76995 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2018-11-01 11:22:14.029 INFO 76995 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-11-01 11:22:14.134 INFO 76995 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2018-11-01 11:22:21.538 INFO 76995 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.useContextualLobCreation(LobCreatorBuilderImpl.java:113) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.makeLobCreatorBuilder(LobCreatorBuilderImpl.java:54) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.<init>(JdbcEnvironmentImpl.java:271) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:114) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) [spring-orm-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at ca.mcgill.ecse321.ridesharing.RideSharingApplication.main(RideSharingApplication.java:13) ~[classes/:na]
Caused by: java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
at org.postgresql.Driver.notImplemented(Driver.java:688) ~[postgresql-42.2.5.jar:42.2.5]
at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1269) ~[postgresql-42.2.5.jar:42.2.5]
... 44 common frames omitted
2018-11-01 11:22:21.539 INFO 76995 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@6c6333cd
2018-11-01 11:22:21.985 INFO 76995 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2018-11-01 11:22:22.044 INFO 76995 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-11-01 11:22:22.244 INFO 76995 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 11:22:22.467 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@14a2f921: startup date [Thu Nov 01 11:22:11 EDT 2018]; root of context hierarchy
2018-11-01 11:22:22.497 WARN 76995 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-11-01 11:22:22.532 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/vehicle/create],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.CarController.createVehicle(java.lang.String,java.lang.String,java.lang.String)
2018-11-01 11:22:22.533 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/vehicle/assignCar/{username}/{id}]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.CarController.assignUserToCar(java.lang.String,java.lang.String)
2018-11-01 11:22:22.533 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/vehicle/{id}],methods=[GET]}" onto public ca.mcgill.ecse321.ridesharing.model.Car ca.mcgill.ecse321.ridesharing.controller.CarController.getVehicle(int)
2018-11-01 11:22:22.540 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/location/getLocation/{id}],methods=[GET]}" onto public ca.mcgill.ecse321.ridesharing.model.Location ca.mcgill.ecse321.ridesharing.controller.LocationController.getLocation(int)
2018-11-01 11:22:22.540 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/location/create],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.LocationController.createLocation(java.lang.String,java.lang.String,double,int)
2018-11-01 11:22:22.540 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/location/addPassenger/{username}/{routeId}/{locationId}],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.LocationController.registerUserToLocation(java.lang.String,int,int)
2018-11-01 11:22:22.541 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/location/assignLocations/{routeId}],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.LocationController.setLocationsToRoute(int)
2018-11-01 11:22:22.543 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.MainController.sayHello()
2018-11-01 11:22:22.544 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/route/create],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.RouteController.createRoute(java.lang.String,int,java.lang.String,java.lang.String)
2018-11-01 11:22:22.544 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/route/getRoutes/{username}],methods=[GET]}" onto public java.util.List<ca.mcgill.ecse321.ridesharing.model.Route> ca.mcgill.ecse321.ridesharing.controller.RouteController.getRoutes(java.lang.String)
2018-11-01 11:22:22.545 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/route/getRoute/{id}],methods=[GET]}" onto public ca.mcgill.ecse321.ridesharing.model.Route ca.mcgill.ecse321.ridesharing.controller.RouteController.getRoute(int)
2018-11-01 11:22:22.546 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/user/getUser/{id}],methods=[GET]}" onto public ca.mcgill.ecse321.ridesharing.model.User ca.mcgill.ecse321.ridesharing.controller.UserController.getId(java.lang.String)
2018-11-01 11:22:22.546 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/user/create],methods=[POST]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.UserController.createUser(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
2018-11-01 11:22:22.546 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/user/addRating/{username}/{rating}],methods=[GET]}" onto public java.lang.String ca.mcgill.ecse321.ridesharing.controller.UserController.addRating(java.lang.String,java.lang.String)
2018-11-01 11:22:22.546 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/user/login/{username}/{password}],methods=[GET]}" onto public boolean ca.mcgill.ecse321.ridesharing.controller.UserController.login(java.lang.String,java.lang.String)
2018-11-01 11:22:22.549 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-01 11:22:22.550 INFO 76995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-11-01 11:22:22.584 INFO 76995 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 11:22:22.585 INFO 76995 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 11:22:22.880 INFO 76995 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-11-01 11:22:22.882 INFO 76995 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-11-01 11:22:22.887 INFO 76995 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-11-01 11:22:22.925 INFO 76995 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-01 11:22:22.929 INFO 76995 --- [ main] c.m.e.r.RideSharingApplication : Started RideSharingApplication in 11.609 seconds (JVM running for 12.36)
UserRepositry.java
package ca.mcgill.ecse321.ridesharing.repository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import ca.mcgill.ecse321.ridesharing.model.*;
import javax.persistence.*;
import java.util.List;
@Repository
public class UserRepository {
@PersistenceContext
EntityManager em;
@Transactional
public double addToRatings(String username, double rating) {
User driver = getUser(username);
driver.increaseNumTrips();
driver.addRating(rating);
double avgRating = computeRating(username);
return avgRating;
}
@Transactional
public double computeRating(String username) {
User driver = getUser(username);
int numPastTrips = driver.getNumTrips();
List<Double> ratings = driver.getRatings();
double avgRating=0;
for (Double rating : ratings) {
avgRating += rating;
}
avgRating /= numPastTrips;
driver.setAvgRating(avgRating);
em.persist(driver);
return avgRating;
}
@Transactional
public User createUser(String username, String password, String firstname, String lastname, String phonenumber,
String city, String address, String role) {
User user = new User();
boolean userValid = validateUsername(username);
if(userValid) {
user.setUsername(username);
}else {
return null;
}
user.setPassword(password);
user.setFirstName(firstname);
user.setLastName(lastname);
user.setPhoneNumber(phonenumber);
user.setCity(city);
user.setAddress(address);
user.setRole(role);
user.setRatings(null);
//user.setAvgRating(0);
//user.setNumTrips(0);
em.persist(user);
return user;
}
public boolean validateUsername(String username) {
Query query = em.createNativeQuery("select username from users;");
@SuppressWarnings("unchecked")
List<String> usernames = (List<String>) query.getResultList();
for (String thisUsername : usernames) {
if(username.equals(thisUsername)) {
return false;
}
}
return true;
}
public boolean login(String username, String password) {
Query query_user = em.createNativeQuery("select username from users;");
@SuppressWarnings("unchecked")
List<String> usernames = (List<String>) query_user.getResultList();
for (String thisUsername : usernames) {
if(username.equals(thisUsername)) {
User user = em.find(User.class, username);
String userPassword = user.getPassword();
if (userPassword.equals(password)) {
return true;
}
}
}
return false;
}
@Transactional
public User getUser(String id) {
return em.find(User.class, id);
}
}
编辑
通过运行mvn clean install -DskipTests
解决了第一个问题。
第二个问题仍然存在,通过REST api在getUser()
中运行UserRepository.java
方法将返回null。
答案 0 :(得分:0)
将属性更改为此:
remote_file_path=$(tac $local_file_name | sed -nr '1,/$file_name/ d; /^\\/ { p; q }')