为什么我的Hibernate中的DAO类没有实体?

9 浏览
0 Comments

为什么我的Hibernate中的DAO类没有实体?

首先,我想说这个应用程序在我想要测试的功能上出现了问题。我基本上想要通过hibernate从MySql中显示所有保存的实体,并在web浏览器中使用thyme leaf将其打印出来。我无法弄清楚我做错了什么。没有异常,但DAO.count显示为0。感谢任何帮助。

DAO:

@Repository("CustomerDAO")

@Transactional

public interface CustomerDAO extends CrudRepository {

public List findAll();

public long count();

}

Controller:

@Controller

public class CustomerController {

private CustomerDAO customerDAO;

@Autowired

public void setCustomerDAO(CustomerDAO customerDAO) {

this.customerDAO = customerDAO;

}

private SessionFactory sessionFactory;

@RequestMapping("/")

public String listCustomers(Model model){

long test = customerDAO.count();

model.addAttribute("answer", test);

List customers = customerDAO.findAll();

model.addAttribute("customers", customers);

return "home";

}

}

实体:

@Entity

@Table(name = "customer")

public class Customer implements Serializable {

@Column(name = "id")

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Id

private int id;

@Column(name = "first_name")

private String firstName;

@Column(name = "last_name")

private String lastName;

@Column(name = "email")

private String email;

public Customer(){}

@Override

public String toString() {

return "Customer{" +

"id=" + id +

", firstName='" + firstName + '\'' +

", lastName='" + lastName + '\'' +

", email='" + email + '\'' +

'}';

}

public Customer(String firstName, String lastName, String email){

this.firstName = firstName;

this.lastName = lastName;

this.email = email;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

Html/thymeleaf:

Title

This didnt work

Application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false

spring.datasource.username=root

spring.datasource.password=metalgear3

spring.datasouce.driver-class-name=com.mysql.jdbc.Driver

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

spring.jpa.hibernate.show-sql=true

spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

entitymanager.packagesToScan = com.luv2code.entity.Customer

Print Trace:

. ____ _ __ _ _

/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v1.5.2.RELEASE)

2017-04-22 15:22:48.047 INFO 1640 --- [ main] com.luv2code.App : Starting App on Ronalds-MacBook-Pro.local with PID 1640 (/Users/ronaldpitt/Desktop/JavaProjects/target/classes started by ronaldpitt in /Users/ronaldpitt/Desktop/JavaProjects)

2017-04-22 15:22:48.050 INFO 1640 --- [ main] com.luv2code.App : No active profile set, falling back to default profiles: default

2017-04-22 15:22:48.334 INFO 1640 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77be656f: startup date [Sat Apr 22 15:22:48 EDT 2017]; root of context hierarchy

2017-04-22 15:22:49.932 INFO 1640 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$36008335] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2017-04-22 15:22:50.374 INFO 1640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)

2017-04-22 15:22:50.403 INFO 1640 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat

2017-04-22 15:22:50.404 INFO 1640 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11

2017-04-22 15:22:50.567 INFO 1640 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2017-04-22 15:22:50.567 INFO 1640 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2237 ms

2017-04-22 15:22:50.765 INFO 1640 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]

2017-04-22 15:22:50.771 INFO 1640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]

2017-04-22 15:22:50.772 INFO 1640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

2017-04-22 15:22:50.772 INFO 1640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]

2017-04-22 15:22:50.773 INFO 1640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]

2017-04-22 15:22:51.672 INFO 1640 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'

2017-04-22 15:22:51.703 INFO 1640 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [

name: default

...]

2017-04-22 15:22:51.825 INFO 1640 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.9.Final}

2017-04-22 15:22:51.827 INFO 1640 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found

2017-04-22 15:22:52.021 INFO 1640 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

2017-04-22 15:22:52.159 INFO 1640 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect

2017-04-22 15:22:52.846 INFO 1640 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@73041b7d'

2017-04-22 15:22:52.851 INFO 1640 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2017-04-22 15:22:53.529 INFO 1640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77be656f: startup date [Sat Apr 22 15:22:48 EDT 2017]; root of context hierarchy

2017-04-22 15:22:53.633 INFO 1640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.luv2code.controller.CustomerController.listCustomers(org.springframework.ui.Model)

2017-04-22 15:22:53.639 INFO 1640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

2017-04-22 15:22:53.639 INFO 1640 --- [ 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.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

2017-04-22 15:22:53.687 INFO 1640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2017-04-22 15:22:53.687 INFO 1640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2017-04-22 15:22:53.756 INFO 1640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2017-04-22 15:22:54.578 INFO 1640 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup

2017-04-22 15:22:54.683 INFO 1640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

2017-04-22 15:22:54.692 INFO 1640 --- [ main] com.luv2code.App : Started App in 17.286 seconds (JVM running for 17.954)

2017-04-22 15:22:56.968 INFO 1640 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'

2017-04-22 15:22:56.968 INFO 1640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started

2017-04-22 15:22:56.997 INFO 1640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms

2017-04-22 15:22:57.170 INFO 1640 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory

0