"No persistence unit with name 'product' found" - java

I recently completed this tutorial: "http://static.springsource.org/docs/Spring-MVC-step-by-step/", but now I want it to work with Hibernate and annotations. I know I'm close but I've hit a roadblock and I can't figure it out. I've posted my code on the Spring forums here. I would greatly appreciate ANY help. Thanks
I'm trying to incorporate annotations and hibernate into it, but I've run into a problem and I can't figure it out. I keep getting errors of "No persistence unit with name 'product' found". ANY help would be greatly appreciated.

You need a META-INF/persistence.xml, with <persistence-unit name="product">. See here
(I usually put it in WEB-INF/classes/META-INF. As noted in the comments, with maven you can place it in src/main/resources/META-INF)

this is a example of file 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="poduct" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
create this file inside "src/main/resources/META-INF" if you use Maven project.

I run onto the same problem and had to manually edit the Web Deployment Assembly Configuration so as to instruct /META-INF/persistence.xml to be deployed under WEB-INF/classes/ (the red box in the attached img was manually edited)

i have the same problem. But when you use the ide(for example eclipse) and configure your server in the ide. When you asosiate the piece and restart the server always have the same error because detected the persistence unit is used, you need delete that part of the server and deploy to the oldway. Enter to the console and install the peace of you project and works!!
And the other is when you check the diagnostic in wls you need said who user is the owner of the jdbc, only select and check (for example, AdminServer) and save and check again!!
:D

Related

How do I resolve “Element bean is not allowed here” when configuring the XML file?

I was creating a simple Java project by importing 4 major jar packages of Spring(beans, core, context and expression, all are ver 5.2.6). However, IDEA kept indicating that "element bean is not allowed here" even though I checked repeatedly that the dependencies are placed correctly in the module section and restarted the IDE. The configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Creating User Object-->
<bean id = "user" class = "com.zhouss.User"><bean/> #bean is where it reports error
</beans>
And here's a snapshot of the dependency jars:
I'm a complete newbie on Spring, how do I resolve the issue?
As it turned out, it was a problem with the particular version of IDEA (2020.3, mac), I have updated to the latest version of 2022.1, mac and the error simply disappears by themselves.
I found very little info online when I tried to resolve this problem, so I find it necessary to write down my findings.

How do I inject EntityManager into a EJB using Bluemix Liberty runtime?

It was supposed to be a trivial thing to do and it's described here (which injects EntityManagerFactory and not EntityManager), here (which does not inject EntityManager, but retrieves it from Persistence object) and here (which actually injects EntityManager in the way I want, and what it says makes sense, since it assumes the data source is called jdbc/<yourdbname>)
However, the third one has a broken link because the source code is not available any more, unfortunately.
My persistence.xml uses JPA 2.1
<?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="mycogdb">
<jta-data-source>jdbc/cogdb</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
and my EJB could not be any simpler
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
#Stateless
public class MyEJB {
#PersistenceContext(unitName="mycogdb")
private EntityManager em;
public void test() {
MyEntity me = new MyEntity();
me.setId(System.currentTimeMillis());
em.persist(me);
}
}
However, it's not being injected
[ERROR ] CWWJP0029E: The server cannot find the <default> persistence
unit in the myapp.war module and the myapp application.
2016-06-17T08:39:02.541-0300
[App/0]
out
[INFO ] FFDC1015I: An FFDC Incident has been created:
"com.ibm.wsspi.injectionengine.InjectionException:
The java:comp/env/mycogdb reference of type javax.persistence.EntityManager
for the null component in the myapp.war module of the myapp application
cannot be resolved. com.ibm.ejs.container.ManagedBeanOBase.injectInstance
134" at ffdc_16.06.17_11.39.02.0.log
Am I missing any configuration in server.xml or in any other config file? Maybe any missing server feature to be enabled? Any help is welcome.
[Edited: Originally noted that the annotation attribute is unitName not name but that ended up not being the problem.]
However the build is performed, make sure the end result is that the persistence.xml ends up in location WEB-INF/classes/META-INF/persistence.xml within the WAR archive.
This is mentioned in JPA Tutorial:
If you package the persistence unit as a set of classes in a WAR file,
persistence.xml should be located in the WAR file’s
WEB-INF/classes/META-INF directory.
Now, that still might leave some questions regarding how to structure the project from both the Eclipse/WDT and Maven angles, as well as integration of your Maven build with the DevOps tool which has Maven integration.
I won't attempt to give a complete answer here across all options, but just noting that it might be more complicated than saying "always put your persistence.xml in src/main/resources" since src/main/java/META-INF appears to me to work in some build use cases.

Maven+Spring Project failing to load dependent xsd files

I've been struggling with this issue for several days now. I am trying to initialize a standard Spring project with the usual spring namespaces: beans, aop, context, util... my current, very initial, beans.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xml
http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xml
http://www.springframework.org/schema/aop http://www.springframework.org/schema/beans/spring-aop.xml
http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util.xml"
</beans>
When I attempt to compile and run the project in Glassfish, I hit an IllegalStateException while invoking org.glassfish.weld.WeldDeployer. Further down it states that the XML of dependent documents must be well formed and shows the first line of one of the dependent files (e.g. spring-beans.xml) which is clearly HTML. After some further digging, I found that indeed in my mavencachedirs where these .xsd files are cached, every single one had identical HTML content referencing a 404 Not Found. I am working offline with internal maven repositories, but this should not be a problem given that my dependent Spring jar files all have the appropriate spring.schemas and spring.handlers files. It simply seems like my project is failing to recognize them.
I'm a bit new to Spring, but I feel like I've done my due diligence in researching this issue. Any suggestions that may point me in the right direction would be greatly appreciated.
UPDATE - Following some of the tips posted in Spring schemaLocation fails when there is no internet connection, I consolidated my spring.handlers and spring.schemas files into two files in src/main/resources/META-INF. Running getResource returns the consolidated version of each file in this directory. That said, this still did not resolve the problem.
UPDATE 2 - I've reproduced this issue at home. I created a new Spring project on Netbeans 7.3.1 using the following archetype:
Group ID: co.ntier
Version: 1.0.2
Artifact ID: spring-mvc-archetype
Repository: http://repo.maven.apache.org/maven2/
Once the project was created, I added a beans.xml file to the classpath and stripped it down to it's most basic form:
<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
/>
I built the project and deployed to Glassfish 4.0 through NetBeans while still connected to the internet. This worked as expected with no errors. However, when I disconnected the computer from the internet and deployed again (AND cleared the cached xsd files from ...Netbeans\Cache\7.3.1\mavencachedirs), I got the following error several times:
WARNING: WELD-001210 Warning when validating file:/C:/Users/Elliott/Desktop/Development/SpringTest/target/SpringTest/WEB-INF/beans.xml#5 against xsd. schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
I understand that Spring is supposed to be reading from the spring.schemas and spring.handlers files to locate these resources, but it clearly isn't! I challenge anybody to attempt to reproduce this issue and demonstrate that this is actually working as it should be. Right now I'm not convinced.
PROGRESS!!
OK. I'm 90% sure I've figured out the problem. When I created the beans.xml file, Netbeans creates a nb-configuration.xml file with the following in it:
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
<config-files>
<config-file>beans.xml</config-file>
</config-files>
<config-file-groups/>
</spring-data>
If I create a servlet in the web.xml file:
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
and rename beans.xml file to SpringDispatcher-servlet.xml, the project compiles and loads the xsd files correctly, whether online or offline.
Since this worked when using the SpringDispatcher-servlet.xml, but not with beans.xml configured by Netbeans, I think it's safe to assume that when Netbeans is configured to load a spring config file through nb-configuration, it fails to reference the required spring.schemas and spring.handlers files in the classpath.
Maybe I'm completely wrong and just super noob-y when it comes to Spring (I am), but it doesn't seem like this behavior is correct. Regardless, at least I have a way to move forward now.

JNDI resource name appended with __pm . Deployment fails

I am trying to run a very simple ear applicaiton . i seem to run into exactly this issue but the discussions here dont work for me JDBC resource name being modified by container (__pm being appended to it)
In my case also the datasource name is being appened by __pm. I have no clue where it comes from but the server logs point to the fact that instead of searching for databasename it searches for databasename__pm and fails the deployment.
Persistence.xml :-
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="FCK-ejbPU" transaction-type="JTA">
<jta-data-source>FCKDatabase</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Glassfish logs :
Error occurred during deployment: Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/FCKDatabase__pm), (applicationName=FCK) }. Please see server.log for more details.
Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method : java.lang.RuntimeException: Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/FCKDatabase__pm), (applicationName=FCK) }
Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/FCKDatabase__pm), (applicationName=FCK) }
As you can see my the JNDI name of my resources is appened with __pm.
Also on the Glassfish server console the resource is correctly shown as FCKDatabase and not FCKDatabase_pm
Any idea where this __pm comes from ?
System:
Netbeans 7.3
Glassfish Server 3.1.2
The problem was that the mysql connector jar file was not placed into the glassfish server. This kicks in a series of problems. The one that I posted above is just one of the error messages that the server logs contains. I suggest going through the entire list of error message and try to debug from there. The following link helped me place the jar files at the correct location:
Connecting a MySQL database to Glassfish classpath is not set or classname is wrong
Blog Class name is wrong or classpath is not set for: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
Since Netbeans 7.1 when the IDE generates the persistence.xml the jta field and glassfish-resouces JDNI name is generated with a prefix java:module/, it has to be deleted in both files, it has to look like jdbc/[anyName]. This was tested on Netbeans 8.1 and Glassfish 4.0.
In the image, the second option shows the generated name, which is invalid en Glassfish because the ':'
Here is another solution which I have found, it will be helpful to anyone.
Copy the resource name which you are facing the problem and follow the below steps.
Login into Glassfish console
Goto Resources
JDBC --> JDBC Resources
Click on New button to add resource
Enter the copied resource into JNDI name field and select the pool name and enter the description.
Click on Ok button
Redeploy the application and check.
In my case I just moved my glassfish-resources.xml file,
from:
my_web_app / src / main / webapp / WEB-INF
to:
my_web_app / src / main / setup
And GlassFish started to recognize the file the created the resources.

XML schema or DTD for logback.xml?

I've seen several discussions on the net about how great it would be to have an XML schema or DTD for logback.xml file to have at least the very basic validation and auto-completion in IDEs like IDEA or Eclipse, but I never saw any solution.
Did you?
As of June 2011, the official documentation states
As shall become clear, the syntax of logback configuration files is extremely flexible. As such, it is not possible specify the allowed syntax with a DTD file or an XML Schema.
There was a brief thread on the topic, but didn't seem to go anywhere.
Just to get rid of the annoying warning in Eclipse add <!DOCTYPE xml> after <?xml version="1.0" encoding="UTF-8"?>.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
To get rid of the exclamation point icon after you fix it, you might need to trigger the validation again by right-clicking the file and choosing Validate.
It is not supported officially according to the documentation, but there is an independent project to provide Schema for Logback
However, due to extreme flexibility of the Logback configuration, Schema cannot support all possible configuration options.
The independent project mentioned by Sergey covered most of my requirements.
However, some elements were missing, I added them on my own fork on on https://github.com/nkatsar/logback-XSD. Hope they will get merged in the main project.
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.padual.com/java/logback.xsd">
from GitHub "An XML Schema Definition for logback"
https://github.com/nkatsar/logback-XSD
Thanks to https://github.com/enricopulatzo/logback-XSD
This will allow eclipse to autocomplete and validate if xml is not using plugins or other extensions mechanisms:
<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns="http://ch.qos.logback/xml/ns/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://ch.qos.logback/xml/ns/logback
https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd
">

Categories

Resources