Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:54:28+00:00 2026-05-31T15:54:28+00:00

I’m trying to figure out how to use @PersistenceUnit as I’ve read it’s a

  • 0

I’m trying to figure out how to use @PersistenceUnit as I’ve read it’s a much better solution than @PersistenceContext. The trouble is…. I can’t figure out how to get it to work properly…

@Controller
public class Content {
    @PersistenceUnit(unitName = "CMTPU")
    public EntityManagerFactory emf;
    public EntityManager em = emf.createEntityManager();

    @RequestMapping(value={"/content/edit*"}, method=RequestMethod.GET)
    public ModelAndView edit(Model model) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("content/edit");

        //get symbols
        List<Symbol> symbols = em.createNamedQuery("Symbol.findAll").getResultList();
        mv.addObject(symbols);

        return mv;
    }
}

My app loaded before I added the //get symbols section and the EntityManager stuff. Now I’m seeing the error SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NullPointerException

I’ve read that I need to define a unitName, but then I’m looking at this documentation and it doesn’t show that being done.

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="CMTPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>CMT_DEV</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

I’m having trouble determining what I’m doing wrong.

update

My model defines the database and all of that as seen below. Do I even need a persistence.xml?

package com.fettergroup.cmt.models;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


    @Entity
    @Table(name = "symbol", catalog = "DATABASE1", schema = "dbo")
    @NamedQueries({
        @NamedQuery(name = "Symbol.findAll", query = "SELECT s FROM Symbol s"),
        @NamedQuery(name = "Symbol.findById", query = "SELECT s FROM Symbol s WHERE s.id = :id"),
        @NamedQuery(name = "Symbol.findBySymbol", query = "SELECT s FROM Symbol s WHERE s.symbol = :symbol"),
        @NamedQuery(name = "Symbol.findByHtmlNumber", query = "SELECT s FROM Symbol s WHERE s.htmlNumber = :htmlNumber"),
        @NamedQuery(name = "Symbol.findByHtmlName", query = "SELECT s FROM Symbol s WHERE s.htmlName = :htmlName"),
        @NamedQuery(name = "Symbol.findByAsciiDec", query = "SELECT s FROM Symbol s WHERE s.asciiDec = :asciiDec"),
        @NamedQuery(name = "Symbol.findByAsciiHex", query = "SELECT s FROM Symbol s WHERE s.asciiHex = :asciiHex")})
    public class Symbol implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @NotNull
        @Column(name = "id")
        private Short id;
        @Size(max = 10)
        @Column(name = "symbol")
        private String symbol;
        @Size(max = 10)
        @Column(name = "html_number")
        private String htmlNumber;
        @Size(max = 10)
        @Column(name = "html_name")
        private String htmlName;
        @Size(max = 10)
        @Column(name = "ascii_dec")
        private String asciiDec;
        @Size(max = 10)
        @Column(name = "ascii_hex")
        private String asciiHex;

        public Symbol() {
        }

        public Symbol(Short id) {
            this.id = id;
        }

        public Short getId() {
            return id;
        }

        public void setId(Short id) {
            this.id = id;
        }

        public String getSymbol() {
            return symbol;
        }

        public void setSymbol(String symbol) {
            this.symbol = symbol;
        }

        public String getHtmlNumber() {
            return htmlNumber;
        }

        public void setHtmlNumber(String htmlNumber) {
            this.htmlNumber = htmlNumber;
        }

        public String getHtmlName() {
            return htmlName;
        }

        public void setHtmlName(String htmlName) {
            this.htmlName = htmlName;
        }

        public String getAsciiDec() {
            return asciiDec;
        }

        public void setAsciiDec(String asciiDec) {
            this.asciiDec = asciiDec;
        }

        public String getAsciiHex() {
            return asciiHex;
        }

        public void setAsciiHex(String asciiHex) {
            this.asciiHex = asciiHex;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Symbol)) {
                return false;
            }
            Symbol other = (Symbol) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "com.project1.models.Symbol[ id=" + id + " ]";
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:54:29+00:00Added an answer on May 31, 2026 at 3:54 pm

    use only

    @Controller
    public class Content {
    
       @PersistenceContext(unitName = "CMTPU")
       public EntityManager em;
    

    The entity manager should be controlled by spring.

    This ins an example, it uses hiberante as persistence provider, but I think you can adapt it.

    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    
    </bean>
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="myPersistenceUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
    </bean>
    

    sample persistance unit, that works with that configuration

    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
        </properties>
    </persistence-unit>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.