Reputation: 29
I tried to configure my hibernate and have this error. I checked all answers on stackoverflow but they did't help. I put cfg file into resources but it did't help.
public class Dbconnect {
public static void main(String[] args) throws Exception {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources( standardRegistry )
.addAnnotatedClass( UserProfile.class )
.getMetadataBuilder()
.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
.build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
}
}
my project structure
my cfg file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mytestdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
upd. Added mvn depedencies and have time zone error.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
error:
окт. 15, 2018 3:19:44 ПП org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.6.Final}
окт. 15, 2018 3:19:44 ПП org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/12/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
INFO: HHH000422: Disabling contextual LOB creation as connection was null
ERROR: The server time zone value 'RTZ 2 (çèìà)' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
Upvotes: 1
Views: 2052
Reputation: 19194
It seems your hibernate.cfg.xml
file is at path src/main/resources
; when maven builds the project, that path will be (by default) at the root of the classpath, so to load it in your context, you should use:
new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml")
EDIT
The new stacktrace shows that the xml file is now found after your change above; unfortunately it couldn't be parsed because of a missing library (jaxb).
Change your pom.xml with the following to add the dependendcy:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
Upvotes: 1