error in creating EntityManager object - weblogic

I am trying to create a EntityManager object for persistence and I am unable to create EntityManager object. Please find the sample code below:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Draco-PU");
EntityManager em = emf.createEntityManager();
I have defined the persistence unit name in persistence.xml file too.
<persistence-unit name="Draco-PU" >
The persistence xml is as defined below:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns
/persistence/persistence_1_0.xsd">
<persistence-unit name="Draco-PU" >
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>DracoDataSource</jta-data-source>
<non-jta-data-source>DracoUnmanagedDataSource</non-jta-data-source>
<class>dk.tdc.soa.smo.draco.db.model.DslServiceEntity</class>
<class>dk.tdc.soa.smo.draco.db.model.DslServiceCatalogEntity</class>
<class>dk.tdc.soa.smo.draco.db.model.History</class>
<class>dk.tdc.soa.smo.draco.db.model.ConfigEntity</class>
<properties>
<property name="openjpa.Log" value="DefaultLevel=WARNING, Runtime=WARNING, Tool=WARNING, SQL=WARNING" />
<!-- <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.OracleDictionary" /> -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
The error faced is :
javax.persistence.PersistenceException: No Persistence provider for EntityManager
named Draco-PU: Provider named kodo.persistence.PersistenceProviderImpl threw
expected exception at create EntityManagerFactory:
org.apache.openjpa.persistence.PersistenceException<openjpa-1.1.1-SNAPSHOT-
r422266:891341 fatal general error>
org.apache.openjpa.persistence.PersistenceException: There was an error when invoking
the static newInstance method on the named factory class
"kodo.jdbc.kernel.KodoJDBCBrokerFactory".
Please help!

Did you declare a "provider" element within your "persistence-unit" element in persistence.xml? It should look something like this:
<persistence-unit name="Draco-PU">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
</persistence-unit>

Create the next structure inside your WEB-INF folder: classes/META-INF, put your persistence.xml file in and restart the server.
Weblogic looks for the file in the build directory.

Related

No Persistence provider for EntityManager named default

I get the following error:
[16:16:45 WARN]: [jakarta.persistence.spi] jakarta.persistence.spi::No valid providers found.
jakarta.persistence.PersistenceException: No Persistence provider for EntityManager named default
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:86) ~[?:?]
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) ~[?:?]
When using:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
EntityManager entityManager = entityManagerFactory.createEntityManager();
I am using the following dependencies:
dependencies {
implementation('org.hibernate:hibernate-core:6.1.6.Final')
implementation('org.postgresql:postgresql:42.5.1')
}
This is my persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="default">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>me.janic.database.PlayerEntity</class>
<class>me.janic.database.ReviveTokenEntity</class>
<class>me.janic.database.StatsEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/1l1c"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
</properties>
</persistence-unit>
</persistence>
And the persistence.xml file is located in META-INF/persistence.xml. I tried different versions of hibernate but I can not get it to work. How is this caused and how can I solve it?

Kundera No Entity metadata found for the class

I get an error "No Entity metadata found for the class" using Kundera a similiar
question is here,
No Entity metadata found for the class
but didn't find an answer (answer put META-INF into classes dir, didn't help me.
This is the error I get
3168 [main] WARN com.impetus.kundera.metadata.KunderaMetadataManager - No Entity metadata found for the class class kundega.rules.Rule. Any CRUD operation on this entity will fail.If your entity is for RDBMS, make sure you put fully qualified entity class name under <class></class> tag in persistence.xml for RDBMS persistence unit. Returning null value.
Exception in thread "main" com.impetus.kundera.KunderaException: java.lang.IllegalArgumentException: Entity object is invalid, operation failed. Please check previous log message for details
at com.impetus.kundera.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:174)
at kundega.rules.AppMain.main(AppMain.java:27)
Caused by: java.lang.IllegalArgumentException: Entity object is invalid, operation failed. Please check previous log message for details
at com.impetus.kundera.graph.ObjectGraphBuilder.getNode(ObjectGraphBuilder.java:101)
at com.impetus.kundera.graph.ObjectGraphBuilder.getObjectGraph(ObjectGraphBuilder.java:75)
at com.impetus.kundera.persistence.PersistenceDelegator.persist(PersistenceDelegator.java:135)
at com.impetus.kundera.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:168)
So here is my files.
Persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="UNIT">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>kundega.rules.Rule</class>
<properties>
<property name="kundera.client.lookup.class" value="com.impetus.client.rdbms.RDBMSClientFactory" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://130.230.141.228:3306/fastory" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="mysql" />
</properties>
</persistence-unit>
</persistence>
Start of Entity class
#Entity
#Table(name="eplrules", schema="fastory")
public class Rule {
#Id
//#GeneratedValue (strategy=GenerationType.AUTO)
#Column (name ="rule_id")
private long ruleId;
#Column (name ="rule")
private String rule;
Main class
public static void main(String[] args) {
System.out.println("start");
BasicConfigurator.configure();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("UNIT");
Rule r = new Rule();
r.setRuleId(103);
r.setRuleName("SomeRuleName");
r.setRule("SomeRule");
EntityManager em = emf.createEntityManager();
em.persist(r);
Query q = em.createQuery("select p from Rule p");
List<Rule> rules = q.getResultList();
em.close();
System.out.println(rules);
System.out.println("stop");
em.close();
emf.close();
}
When I got this I checked:
<class>package.name.DaoClass</class>
was defined in persistence.xml, this resolved the error. Also check
<property name="kundera.annotations.scan.package" value="package.name"/>
matches correctly to your package name.
"kundega.rules.Rule" package name is correct? Or is it "kundera.rules.Rule" ?

trying to get a sample petstore using spring mybatis project working in intellij

I have downloaded the sample from http://mybatis.github.io/spring/sample.html.
1)I then open the pom.xml and imported it into intellij
2)I added a spring MVC facet
3) i added a web facet
4) i am using intellij 12.1.6
Once done, the autowiring is failing. I am trying to learn this new framework
All the service autowires have an error similar to::
Could not autowire. No beans of 'LineItemMapper' type found
public class OrderService {
#Autowired
private ItemMapper itemMapper;
#Autowired
private OrderMapper orderMapper;
#Autowired
private SequenceMapper sequenceMapper;
#Autowired
private LineItemMapper lineItemMapper;
I am thinking it is something i have setup incorrectly in my project.
this is the provided applicationCOntext from the example
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- in-memory database and a datasource -->
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:database/jpetstore-hsqldb-schema.sql"/>
<jdbc:script location="classpath:database/jpetstore-hsqldb-dataload.sql"/>
</jdbc:embedded-database>
<!-- transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="org.mybatis.jpetstore.service" />
<!-- enable autowire -->
<context:annotation-config />
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven />
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.jpetstore.persistence" />
</bean>
</beans>
this is a zip of the project https://www.dropbox.com/s/lohr3udnm0oa2hn/mybatis-jpetstore-6.0.1-sources.zip
Hopefully someone can point me to what i am doing incorrectly
Thanks for any help
The first thing to do is to try using the latest version of IntelliJ, released earlier this week. It has improved support for spring.
The underlying problem is that you need to set up IntelliJ's spring facet and assign the application context groups.

sl4j/logback under weblogic

I'm trying to configure sl4j/logback under Weblogic12.
I deploy ear file, which has war file, which has WEB-INF\classes\logback.xml
Here is the config:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
My code to log :
private static final Logger logger = LoggerFactory.getLogger(FrontEndServlet.class);
//......
logger.info("info test");
logger.debug("debug test");
logger.error("error test");
What I see in the standart output is :
ьрщ 14, 2012 5:09:29 PM .....FrontEndServlet doPost
INFO: info test
ьрщ 14, 2012 5:09:29 PM .....FrontEndServlet doPost
SEVERE: error test
So, it looks like config file is not picked up.
What am I doing wrong?
The problem is discussed here in detail: https://stagingthinking.wordpress.com/2012/06/02/annoying-slf4j-problem-in-weblogic-server-12c/
The exact package you need to put to the prefer-application-packages mechanism is org.slf4j, like this:
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-application>
<prefer-application-packages>
<package-name>org.slf4j</package-name>
</prefer-application-packages>
</weblogic-application>
Note: Also this question is already answered, I want to add that you should also add prefer-application-resources.
Answer: Add a file called META-INF/weblogic-application.xml to your ear, containing both prefer-application-packages and prefer-application-resources!
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-application
xmlns="http://xmlns.oracle.com/weblogic/weblogic-application"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.5/weblogic-application.xsd"
version="6">
<!-- http://www.torsten-horn.de/techdocs/jee-oracleweblogic.htm -->
<prefer-application-packages>
<package-name>org.slf4j.*</package-name>
</prefer-application-packages>
<!-- if not using prefer-application-resources you will get a warning like this: -->
<!-- Class path contains multiple SLF4J bindings -->
<!-- SLF4J: Found binding in [jar:file:/C:/wls1211/modules/org.slf4j.jdk14_1.6.1.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] -->
<prefer-application-resources>
<resource-name>org/slf4j/impl/StaticLoggerBinder.class</resource-name>
</prefer-application-resources>
</weblogic-application>
The problem was - sl4j did not pick up logback and used Weblogic's slf4j-jdk logging instead. Can be fixed with Weblogic's config weblogic-application.xml, option prefer-application-packages
Alternatively or if you have problems with more than just slf4j, you could use
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
Instead of
<prefer-application-packages>
<package-name>org.slf4j.*</package-name>
</prefer-application-packages>
Source: Oracle
Environment: Weblogic 12.2.1
Logging Framework : Slf4j and Logback
Requirement : Log to a file of my choosing (per application) as well as Weblogic server logs
Using the <prefer-application-packages/> or <prefer-web-inf-classes> in weblogic.xml did not satisfy the requirement. In my testing, using one or the other tags (you can't use both) will result in the application logback.xml to be picked up and logging will go to the file defined in logback.xml. However, the typical STDOUT defintion using logback's ConsoleAppender will not log to the server logs.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
Removing the following from weblogic.xml
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
</wls:prefer-application-packages>
will result in using the bundled SLF4j binding, which in Weblogic 12.2.1, is Java Util logging. In this case, log statements will go to the server logs and not to the file definition in the application level logback.xml. In my research, it appears at one time, some version of Weblogic 12 allowed the internal SLF4j to be bound to Log4j but was removed in one of the minor releases. This was my case; I did not have the option of enabling Log4j as the primary logging Framework in Weblogic through the Admin console. I am fairly sure this wouldn't have helped me, but I did want to note it because several documents I read indicated this would be available.
After much research and fighting configuration with weblogic.xml, configuration of POM (exclusions etc) and trying to use different bindings and bridges, I was unable to achieve the logging configuration that I wanted. It appears that Weblogic's slf4j is bound to Java utility logging, for better or worse. If you choose your own implementation of slf4j and binding (in my case Logback), there is no way that I could find to route those messages to Weblogic server logs through configuration. There can only be one binding in slf4j, and although many frameworks can be routed to that one binding, (I found this diagram useful) Weblogic 12.2.1 only employs Java util logging binding, there is no way to (at the application configuration level) to wire Weblogic to use the Logback binding that you provide to log to its server logs. There might be some way to use log4j and bridges to accomplish this, but for me that's entirely too much bloat and configuration to accomplish a simple logging task.
Giving up on trying to conquer this by configuration, I decided to simply write my own logback appender that translates a logging event into a JUL logging event. I replaced the standard STDOUT definition seen in many Logback examples with my own implementation of Logback's AppenderBase. At this point I can now log using per application logging configuration and also log to the Weblogic Server log.
Relevant POM Dependencies:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
weblogic.xml (Note here that Hibernate comes with JbossLogging which will bridge to slf4j automatically)
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/2.0/weblogic-web-app.xsd">
<jsp-descriptor>
<keepgenerated>true</keepgenerated>
<debug>true</debug>
</jsp-descriptor>
<context-root>YourContextRoot</context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>ch.qos.logback.*</wls:package-name>
<wls:package-name>org.jboss.logging.*</wls:package-name>
<wls:package-name>org.slf4j.*</wls:package-name>
</wls:prefer-application-packages>
<wls:prefer-application-resources>
<wls:resource-name>org/slf4j/impl/StaticLoggerBinder.class</wls:resource-name>
</wls:prefer-application-resources>
</wls:container-descriptor>
Logback AppenderBase implementation
import java.util.logging.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
public class WeblogicAppender extends AppenderBase<ILoggingEvent> {
private final Logger logger = Logger.getLogger(WeblogicAppender.class.getName());
ILoggingEvent event = null;
#Override
protected void append(ILoggingEvent event) {
this.event = event;
logger.log(getJULLevel(), event.getFormattedMessage());
}
private java.util.logging.Level getJULLevel() {
if (this.event == null) {
return java.util.logging.Level.SEVERE;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.ALL) {
return java.util.logging.Level.ALL;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.DEBUG) {
return java.util.logging.Level.FINE;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.ERROR) {
return java.util.logging.Level.SEVERE;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.INFO) {
return java.util.logging.Level.INFO;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.TRACE) {
return java.util.logging.Level.FINEST;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.WARN) {
return java.util.logging.Level.WARNING;
} else if (this.event.getLevel() == ch.qos.logback.classic.Level.OFF) {
return java.util.logging.Level.OFF;
} else {
return java.util.logging.Level.INFO;
}
}
}
Logback.xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="com.your.package.WeblogicAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger: LineNumber:%L - %message%n</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>yourlog.log
</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>yourlog.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>25MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger: LineNumber:%L - %message%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Hopefully I can save others some of the pain that I went through trying to get this working the way I wanted.

Config NHibernate

I use config file for NHibernate .
I want define more than one session-factrory in same config file .
I do it like this :
`
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="test1">
all properties
</session-factory>
<session-factory name="test2">
all properties
</session-factory>'
The application throw exception :
nhibernate.cfg.HibernateConfigException : An exception occurred parsing configuration : The element 'hibernate-configuration' has invalid child element 'session-factory'
You can't define two session factories in a single config file (the schema does not allow it, and NHibernate doesn't provide a way to access them anyway)
Use separate files or, better yet, one of the code-based approaches, which are more flexible.
See http://fabiomaulo.blogspot.com/2009/07/nhibernate-fluent-configuration.html and http://fabiomaulo.blogspot.com/2009/07/nhibernate-configuration-through.html