Bài 2: Kết nối dữ liệu bằng Hibernate Framework

Tiếp theo loạt bài xây dựng website bán hàng bằng sử dụng  Spring Framework thì mình sẽ tiếp tục trình bày về phần tạo các đối tượng và kết nối dữ liệu bằng Hibernate Framework.

Việc đầu tiên là bạn sẽ nghĩ đến việc xây dựng các lớp cơ bản cũng như quan hệ giữa các lớp với nhau, đối với một website thương mại điện tử như mình hướng dẫn ở đây thì nhất định phải có hai lớp chính là sản phẩm và danh mục sản phẩm. Mình sẽ sử dụng Spring Framework kết hợp kết nối dữ liệu bằng Hibernate Framework để xây dựng website bán hàng.

Lớp danh mục sản phẩm (Category.java) bao gồm các thuộc tính như mã danh mục và tên danh mục như sau:

Còn đối với lớp sản phẩm (Product.java) sẽ có các thuộc tính cơ bản như mã sản phẩm, tên sản phẩm, giá bán, hình ảnh, mô tả ngắn… ngoài ra bạn có thể thêm một số thuộc tính khác để phù hợp với website của bạn.

Xong phần tạo ra các đối tượng, tiếp theo sẽ tiến hành viết một số phương thức cơ bản bằng Hibernate Framework, mình sẽ ví dụ hàm lấy danh sách danh mục như sau, nhưng trước đó bạn cần cấu hình lại trong tập tin hibernate.cfg cho phù hợp.

Tạo lớp HibernateUtil.java như sau:

Phương thức lấy tất cả danh mục sản phẩm sử dụng kết nối dữ liệu bằng Hibernate Framework:

Download source code ngay tại đây.

Download source link 1:

Kết nối dữ liệu bằng Hibernate Framework

Download source link 2:

Kết nối dữ liệu bằng Hibernate Framework

(Tác giả: Tùng Dương)

52 thoughts on “Bài 2: Kết nối dữ liệu bằng Hibernate Framework

  1. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on model.Product.category references an unknown entity: java.util.Locale$Category

    Minh bi loi nay khong biet nhu nao.Add giup minh voi

  2. Exception in thread “main” java.lang.ExceptionInInitializerError
    at util.HibernateUtil.(HibernateUtil.java:22)
    at dao.CategoryDao.getAllcategory(CategoryDao.java:24)
    at dao.CategoryDao.main(CategoryDao.java:32)
    Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at util.HibernateUtil.(HibernateUtil.java:20)
    … 2 more
    Caused by: java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    … 16 more
    C:\Users\Computer\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

  3. mình góp ý là admin nên vừa code vừa nói thì người xem dẽ hiểu hơn có những đoạn code mỉnh chẳng hiểu nó để làm gì

  4. chào ad,
    ở đây e không muốn kết nối với mysql mà muốn kết nối với postgres
    kết nối thì được rồi, nhưng tại sao khi get thì không thất dữ liệu

    1. Chào bạn, bạn xem kết nối với PostgreSQL đã đủ thư viện, đúng cấu hình db hay chưa, hoặc có thể câu truy vấn bạn bị sai cú pháp chẳng hạn.

  5. Aug 21, 2017 10:33:51 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {5.2.2.Final}
    Aug 21, 2017 10:33:52 PM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Aug 21, 2017 10:33:52 PM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Aug 21, 2017 10:33:53 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
    WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
    Aug 21, 2017 10:33:54 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    Aug 21, 2017 10:33:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
    Aug 21, 2017 10:33:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001005: using driver [net.sourceforge.jtds.jdbc.Driver] at URL [jdbc:jtds:sqlserver://localhost:1433;instance=shopweb]
    Aug 21, 2017 10:33:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001001: Connection properties: {}
    Aug 21, 2017 10:33:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001003: Autocommit mode: false
    Aug 21, 2017 10:33:55 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections
    INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
    Aug 21, 2017 10:34:02 PM org.hibernate.dialect.Dialect
    INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
    Aug 21, 2017 10:34:02 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
    INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
    Aug 21, 2017 10:34:04 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
    INFO: HHH000397: Using ASTQueryTranslatorFactory
    Exception in thread “main” java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Category is not mapped [from Category]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:636)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
    at com.sun.proxy.$Proxy26.createQuery(Unknown Source)
    at dao.CategoryDao.getAllcategory(CategoryDao.java:13)
    at dao.CategoryDao.main(CategoryDao.java:20)
    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Category is not mapped [from Category]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
    at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:152)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:526)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:628)
    … 9 more
    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Category is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3690)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3579)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:718)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:574)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
    … 15 more
    lỗi như này là sao ad mong ad giúp

    1. Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Category is not mapped
      Bạn kiểm tra lại trong file hibenate.cfg.xml có mapping đối tượng Category chưa nha.

  6. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database ‘shopcars’
    Lỗi này là do gì ạ?
    Em đã tạo 1 database shopcars trong Navicat rồi ạ

          1. Aug 26, 2017 10:08:58 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
            INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
            Aug 26, 2017 10:08:59 PM org.hibernate.Version logVersion
            INFO: HHH000412: Hibernate Core {4.3.1.Final}
            Aug 26, 2017 10:08:59 PM org.hibernate.cfg.Environment
            INFO: HHH000206: hibernate.properties not found
            Aug 26, 2017 10:08:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
            INFO: HHH000021: Bytecode provider name : javassist
            Aug 26, 2017 10:08:59 PM org.hibernate.cfg.Configuration configure
            INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
            Aug 26, 2017 10:08:59 PM org.hibernate.cfg.Configuration getConfigurationInputStream
            INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
            Aug 26, 2017 10:08:59 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
            WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
            Aug 26, 2017 10:08:59 PM org.hibernate.cfg.Configuration doConfigure
            INFO: HHH000041: Configured SessionFactory: null
            Aug 26, 2017 10:09:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
            WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
            Aug 26, 2017 10:09:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
            INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/shopcars]
            Aug 26, 2017 10:09:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
            INFO: HHH000046: Connection properties: {useUnicode=true, user=root, password=****, characterEncoding=UTF-8}
            Aug 26, 2017 10:09:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
            INFO: HHH000006: Autocommit mode: false
            Aug 26, 2017 10:09:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
            INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
            Exception in thread “main” java.lang.ExceptionInInitializerError
            at Helper.HibernateUtil.(HibernateUtil.java:23)
            at Data.CategoryData.getAllcategory(CategoryData.java:23)
            at Data.CategoryData.main(CategoryData.java:33)
            Caused by: org.hibernate.exception.SQLGrammarException: Error calling Driver#connect
            at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
            at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
            at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
            at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
            at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
            at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
            at Helper.HibernateUtil.(HibernateUtil.java:21)
            … 2 more
            Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database ‘shopcars’
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
            at com.mysql.jdbc.Util.getInstance(Util.java:386)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
            at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4615)
            at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1302)
            at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
            at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
            at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
            at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
            at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
            at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
            at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
            … 16 more
            C:\Users\hhumo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
            BUILD FAILED (total time: 6 seconds)

            Nguyên văn lỗi nà ad 🙁

          2. code của bạn mình đã nhận được, mình chạy mình thường nha bạn.
            Bạn kiểm tra một lần nữa database có đúng không. Mình có tình để sai database
            thì kết quả là giống như lỗi mà bạn up lên.

  7. em bị lỗi này
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
    INFO: HHH000262: Table not found: product
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
    INFO: HHH000262: Table not found: product
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
    INFO: HHH000262: Table not found: product
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
    ERROR: HHH000388: Unsuccessful: create table product (productID bigint not null, productDescription varchar(255), productImage varchar(255), productName varchar(255), productPrice double not null, categoryID bigint, primary key (productID))
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
    ERROR: Incorrect syntax near the keyword ‘not’.
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
    ERROR: HHH000388: Unsuccessful: alter table product add constraint FK_hfdpy6e6md3osou6tinpaysjj foreign key (categoryID) references category
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
    ERROR: Cannot find the object “product” because it does not exist or you do not have permissions.
    Sep 17, 2017 9:00:55 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
    INFO: HHH000232: Schema update complete
    nó chỉ tạo được bảng cho category và không được tạo được bảng cho product anh xem giúp em với

  8. Hibernate:
    select
    category0_.categoryID as category1_0_,
    category0_.categoryName as category2_0_
    from
    category category0_
    Exception in thread “main” org.hibernate.InstantiationException: No default constructor for entity: : model.Category
    at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:120)
    at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:737)
    at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4755)
    at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1387)
    at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1375)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1618)
    at org.hibernate.loader.Loader.getRow(Loader.java:1514)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2551)
    at org.hibernate.loader.Loader.doList(Loader.java:2537)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
    at org.hibernate.loader.Loader.list(Loader.java:2362)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at dao.CategoryDao.getAllcategory(CategoryDao.java:25)
    at dao.CategoryDao.main(CategoryDao.java:31)

    Bạn xử lí giúp mình lỗi này đc ko ad?

  9. run:
    Nov 13, 2017 1:04:09 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
    Nov 13, 2017 1:04:09 AM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {4.3.1.Final}
    Nov 13, 2017 1:04:09 AM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Nov 13, 2017 1:04:09 AM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Nov 13, 2017 1:04:09 AM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
    Nov 13, 2017 1:04:09 AM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
    Nov 13, 2017 1:04:10 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
    WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    Nov 13, 2017 1:04:10 AM org.hibernate.cfg.Configuration doConfigure
    INFO: HHH000041: Configured SessionFactory: null
    Nov 13, 2017 1:04:10 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at until.HibernateUtil.(HibernateUtil.java:22)
    at Dao.categoryDao.getAllcategory(categoryDao.java:22)
    at Dao.categoryDao.main(categoryDao.java:30)
    Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.microsoft.sqlserver.jdbc.SQLServerDriver]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at until.HibernateUtil.(HibernateUtil.java:20)
    … 2 more
    Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.microsoft.sqlserver.jdbc.SQLServerDriver
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
    … 17 more
    C:\Users\DUC ANH\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 10 seconds)
    bạn ơi mình sử dụng sqlserver bạn giúp mình được không ạ

    1. Chào bạn,
      Bạn gửi class config hibernate mình xem thử. Mình thấy lỗi này
      “org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.microsoft.sqlserver.jdbc.SQLServerDriver]”

  10. run:
    Nov 13, 2017 2:02:46 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
    Nov 13, 2017 2:02:46 AM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {4.3.1.Final}
    Nov 13, 2017 2:02:46 AM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Nov 13, 2017 2:02:46 AM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Nov 13, 2017 2:02:46 AM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
    Nov 13, 2017 2:02:46 AM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
    Nov 13, 2017 2:02:46 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
    WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    Nov 13, 2017 2:02:46 AM org.hibernate.cfg.Configuration doConfigure
    INFO: HHH000041: Configured SessionFactory: null
    Nov 13, 2017 2:02:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
    Nov 13, 2017 2:02:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=book]
    Nov 13, 2017 2:02:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000046: Connection properties: {user=sa, password=****}
    Nov 13, 2017 2:02:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000006: Autocommit mode: false
    Nov 13, 2017 2:02:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
    Nov 13, 2017 2:02:47 AM org.hibernate.dialect.Dialect
    INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at until.HibernateUtil.(HibernateUtil.java:23)
    at Dao.categoryDao.getAllcategory(categoryDao.java:22)
    at Dao.categoryDao.main(categoryDao.java:30)
    Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: model.Category.listProduct[model.Product]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at until.HibernateUtil.(HibernateUtil.java:21)
    … 2 more

    1. Chào bạn, bạn xem phần quan hệ giữa bảng sản phẩm và danh mục thử
      Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:

  11. Em không kết nối được với database và bị lỗi này:
    Dec 12, 2017 4:13:11 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
    Dec 12, 2017 4:13:11 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {4.3.1.Final}
    Dec 12, 2017 4:13:11 PM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Dec 12, 2017 4:13:11 PM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Dec 12, 2017 4:13:11 PM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
    Dec 12, 2017 4:13:11 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
    Dec 12, 2017 4:13:11 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
    WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    Dec 12, 2017 4:13:11 PM org.hibernate.cfg.Configuration doConfigure
    INFO: HHH000041: Configured SessionFactory: null
    Dec 12, 2017 4:13:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
    Dec 12, 2017 4:13:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/webshop]
    Dec 12, 2017 4:13:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000046: Connection properties: {useUnicode=true, user=root, password=****, characterEncoding=UTF-8}
    Dec 12, 2017 4:13:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH000006: Autocommit mode: false
    Dec 12, 2017 4:13:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at util.HibernateUtil.(HibernateUtil.java:20)
    at dao.CategoryDAO.getAllcategory(CategoryDAO.java:21)
    at dao.CategoryDAO.main(CategoryDAO.java:30)
    Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at util.HibernateUtil.(HibernateUtil.java:18)
    … 2 more
    Caused by: java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    … 16 more
    C:\Users\PhuTranPC\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 1 second)

    Hibernate.cfg của em:

    org.hibernate.dialect.MySQLDialect
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/webshop
    root
    1234
    true
    UTF-8
    root
    root
    thread
    update
    true
    true

    1. org.hibernate.dialect.MySQLDialect
      com.mysql.jdbc.Driver
      jdbc:mysql://localhost:3306/webshop
      root
      1234
      true
      UTF-8
      root
      root
      thread
      update
      true
      true

  12. Em làm được rồi nha Admin, nãy em ghi thêm 1 lần USER với PASS ở trên nên bị lỗi không connect vào mysql được

  13. Tại sao ko nói gì? Phải giải thích thì độc giả mới hiểu tại sao phải dùng cái x, tại sao phải dùng cái y chứ a. :(.
    Nhiều lúc e ko hiểu a làm gì cả :D.

    1. Chào bạn, cảm ơn bạn đã quan tâm và đóng góp ý kiến.
      Thời gian tới mình sẽ ra các bài viết giải thích kỹ hơn.
      Chúc bạn một ngày vui!

  14. Trần Trung Hiếu

    - Edit

    Reply

    Mình đang làm 1 bài kết nối hibernate thì bị lỗi này, bạn giúp mình với 😀
    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 org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.example.demo.model.MyUserType.userTypeName
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
    Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.example.demo.model.MyUserType.userTypeName
    at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:322) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1892) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:911) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:738) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    … 16 common frames omitted

    Process finished with exit code 1

    1. Mình thấy lỗi này “Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.example.demo.model.MyUserType.userTypeName” bạn xem lại đối tượng MyUserType map quan hệ đúng chưa nha.

  15. Nguyễn Tuấn Khải

    - Edit

    Reply

    Vì sao khi em tạo Hibernate Reverse thì nó báo lỗi là “Cannot establish database connection with selected Hibernate Configuration file. Please verify the database connection details in hibernate.cfg.xml.”

  16. Mình bị lỗi như vậy admin có thể giúp đỡ mình được không ạ.
    Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at util.HibernateUtil.(HibernateUtil.java:21)
    … 2 more
    Caused by: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    … 16 more
    Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)
    … 29 more
    C:\Users\ASUS\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 1 second)

    1. Lỗi “java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long”
      Bạn kiểm tra lại kiểu dữ liệu nha!.

  17. Ngô Phạm Phú Đức

    - Edit

    Reply

    Exception in thread “main” java.lang.ExceptionInInitializerError
    at util.HibernateUtil.(HibernateUtil.java:22)
    at dao.CategoryDAO.getAllcategory(CategoryDAO.java:22)
    at dao.CategoryDAO.main(CategoryDAO.java:31)
    Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at util.HibernateUtil.(HibernateUtil.java:20)
    … 2 more
    Caused by: java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    … 16 more

  18. Nguyễn Nhật Kiên

    - Edit

    Reply

    run:
    Sep 05, 2018 1:53:32 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
    Sep 05, 2018 1:53:32 AM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {4.3.1.Final}
    Sep 05, 2018 1:53:32 AM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Sep 05, 2018 1:53:32 AM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Sep 05, 2018 1:53:32 AM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
    Sep 05, 2018 1:53:32 AM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at util.HibernateUtil.(HibernateUtil.java:23)
    at dao.CategoryDAO.getAllcategory(CategoryDAO.java:21)
    at dao.CategoryDAO.main(CategoryDAO.java:31)
    Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177)
    at util.HibernateUtil.(HibernateUtil.java:21)
    … 2 more
    Caused by: org.dom4j.DocumentException: Error on line 2 of document : The processing instruction target matching “[xX][mM][lL]” is not allowed. Nested exception: The processing instruction target matching “[xX][mM][lL]” is not allowed.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
    … 10 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    1. Bạn kiểm tra lại file config nha nó bào lỗi “Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xm”

Bình luận

Loading...