java - Gradle Hibernate Configuration Requires Mapped Classes -
i'm using gradle manage dependencies hibernate (i using maven), i've encountered strange. when try save trivial instance of annotated class database wind getting mappingexception
saying said class not mapped. situation unique because of way i've been storing instances. assume situation this: have annotated hibernate class saved using session
entitymanager
using saveorupdate()
. have persistance.xml, no hibernate configuration files. i'm relying on entitymanager
s auto detection of mapped classes feed session
can recognized annotated. i'm using hibernate 4.3.8. code worked before switch. method calls take place in transactional context , transaction committed , session flushed.
exception:
caused by: org.hibernate.mappingexception: unknown entity: com.gmail.sarah.project.rank
gradle dependencies:
compile "org.hibernate:hibernate-core:4.3.8.final" compile "org.hibernate:hibernate-entitymanager:4.3.8.final" compile "org.hibernate:hibernate-ehcache:4.3.8.final" compile "mysql:mysql-connector-java:5.1.35"
edit (a test case has same problem): main class:
package com.gmail.physicistsarah.gradletestproject.core; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.persistence; import javax.persistence.table; import org.hibernate.session; import org.hibernate.transaction; public class init { /** * @param args command line arguments */ public static void main(string[] args) { entitymanagerfactory factory = persistence.createentitymanagerfactory("test_project"); entitymanager manager = factory.createentitymanager(); session session = manager.unwrap(session.class); transaction transaction = session.gettransaction(); transaction.begin(); session.saveorupdate(new person("carl", "gauss")); session.saveorupdate(new person("benoit", "mandelbrot")); transaction.commit(); factory.close(); } @entity @table(name = "person") public static class person { @column(name = "first_name", nullable = false) private final string firstname; @column(name = "last_name", nullable = false) private final string lastname; public person(string firstname, string lastname) { this.firstname = firstname; this.lastname = lastname; } }
} persistence.xml:
<?xml version="1.0" encoding="utf-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="test_project" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test_project_db?zerodatetimebehavior=converttonull"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.driver"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.nocacheprovider"/> <property name="javax.persistence.schema-generation.database.action" value="create"/> </properties> </persistence-unit>
build.gradle:
apply plugin: 'java' apply plugin:'application' sourcecompatibility = '1.8' [compilejava, compiletestjava]*.options*.encoding = 'utf-8' // netbeans automatically add "run" , "debug" tasks relying on // "mainclass" property. may define property prior executing // tasks passing "-pmainclass=<qualified_class_name>" argument. // // note however, may define own "run" , "debug" task if // prefer. in case netbeans not add these tasks may rely on // own implementation. mainclassname = "com.gmail.physicistsarah.gradletestproject.core.init" if (!hasproperty('mainclass')) { ext.mainclass = 'com.gmail.physicistsarah.gradletestproject.core.init' } task sourcesjar(type: jar, dependson: classes) { classifier = 'sources' sourcesets.main.allsource } task javadocjar(type: jar, dependson: javadoc) { classifier = 'javadoc' javadoc.destinationdir } artifacts { archives sourcesjar archives javadocjar } repositories { mavencentral() // may define additional repositories, or remove "mavencentral()". // read more repositories here: // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories } dependencies { // todo: add dependencies here ... // can read more how add dependency here: // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies compile "org.hibernate:hibernate-core:4.3.8.final" compile "org.hibernate:hibernate-entitymanager:4.3.8.final" compile "org.hibernate:hibernate-ehcache:4.3.8.final" compile "mysql:mysql-connector-java:5.1.35" testcompile group: 'junit', name: 'junit', version: '4.10' }
exception:
info: hhh000400: using dialect: org.hibernate.dialect.mysql5dialect exception in thread "main" org.hibernate.mappingexception: unknown entity: com.gmail.physicistsarah.gradletestproject.core.init$person @ org.hibernate.internal.sessionfactoryimpl.getentitypersister(sessionfactoryimpl.java:1096) @ org.hibernate.internal.sessionimpl.getentitypersister(sessionimpl.java:1443) @ org.hibernate.engine.internal.foreignkeys.istransient(foreignkeys.java:242) @ org.hibernate.event.internal.abstractsaveeventlistener.getentitystate(abstractsaveeventlistener.java:511) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.performsaveorupdate(defaultsaveorupdateeventlistener.java:100) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90) @ org.hibernate.internal.sessionimpl.firesaveorupdate(sessionimpl.java:684) @ org.hibernate.internal.sessionimpl.saveorupdate(sessionimpl.java:676) @ org.hibernate.internal.sessionimpl.saveorupdate(sessionimpl.java:671) @ com.gmail.physicistsarah.gradletestproject.core.init.main(init.java:23)
the persistence.xml (and meta-inf package) in resources/main folder.
you have many errors in code:
- entities may not nested classes
- entities must have no-arg constructor
- entities fields may not final
- entities must have attribute annotated
@id
entities must listed in persistence.xml file:
<class>com.foo.bar.entityclassname</class>
Comments
Post a Comment