persistenceUnit/Hibernate
Connection to persistenceUnit/Hibernate failed
Could not create connection to database server. Attempted reconnect 3 times
I have this error and when i go to jpa console and run commands, i get this:
access denied for user 'root'#'localhost' (using password: YES)
Despite, i can see my database in database tab in intellij. And also it created entityclass.
So, why do i have those errors?
It did not create persistance.xml so i created it and put to src/web-inf
<persistence-unit name="persistenceUnit">
<class>models.MovieEntity</class>
<properties>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.user"/>
<property name="eclipselink.jdbc.password"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username"/>
<property name="hibernate.connection.password"/>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName"/>
<property name="openjpa.ConnectionPassword"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.user"/>
<property name="toplink.jdbc.password"/>
</properties>
</persistence-unit>
it generated those lines but sone of them are red, errors. SAme password for all for example?
Also, i get those errors:
ava.lang.RuntimeException: org.hibernate.AnnotationException: No identifier specified for entity: com.models.MovieEntity
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
my entity
#Entity
#Table(name = "movie", schema = "mysql", catalog = "")
public class MovieEntity {
private int id;
private String title;
private String actors;
#Basic
#Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#Id
#Column(name = "Title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Basic
#Column(name = "Actors")
public String getActors() {
return actors;
}
public void setActors(String actors) {
this.actors = actors;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MovieEntity that = (MovieEntity) o;
if (id != that.id) return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (actors != null ? !actors.equals(that.actors) : that.actors != null) return false;
return true;
}
#Override
public int hashCode() {
int result = id;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (actors != null ? actors.hashCode() : 0);
return result;
}
}
entity class gives error of no catalog.
Seen the your configuration I can see that you have the username and password empty, now probably the persistance.xml that you are posted was a example bat one of your error access denied for user 'root'#'localhost' (using password: YES) follow me to advice you to check if your database was configured with a password and use it (I'm not crazy in a standard installation without configuration on linux many times I had empty password). Then I can see that you have #Id on title properties when probably the your sql for tabel generation, requires that id was the primary key.
I hope that this can help you
The database seems to be missing in the connection string
Most likely your Entity has no field annotated with #Id
Your actual error is :
No identifier specified for entity: com.models.MovieEntity
You must specify an id in com.models.MovieEntity class
#javax.persistence.Id
private Long id;
Related
I am working on a new project with postgreSQL 13 and the auto generation of the data schema does not work. My approach is to use annotations on my entities as I always did before.
Here is one of my entities and my persistence.xml file:
#Entity
public class User implements Serializable {
#OneToOne
private Agent agent;
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idUser;
private ProfilUser profilUser;
private LocalDate dateCreation;
private LocalDate dateActivation;
private LocalDate date_desactivation;
private LocalDate date_premiereConnexion;
#ManyToMany
private List <ProfilUser> userProfils;
public Long getId() {
return idUser;
}
public void setId(Long id) {
this.idUser = id;
}
#Override
public int hashCode() {
int hash = 0;
hash += (idUser != null ? idUser.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the idUser fields are not set
if (!(object instanceof User)) {
return false;
}
User other = (User) object;
if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser))) {
return false;
}
return true;
}
#Override
public String toString() {
return "com.weyetech.gestionstock.entities.User[ id=" + idUser + " ]";
}
public LocalDate getDateCreation() {
return dateCreation;
}
public void setDateCreation(LocalDate dateCreation) {
this.dateCreation = dateCreation;
}
public LocalDate getDateActivation() {
return dateActivation;
}
public void setDateActivation(LocalDate dateActivation) {
this.dateActivation = dateActivation;
}
public LocalDate getDate_desactivation() {
return date_desactivation;
}
public void setDate_desactivation(LocalDate date_desactivation) {
this.date_desactivation = date_desactivation;
}
public LocalDate getDate_premiereConnexion() {
return date_premiereConnexion;
}
public void setDate_premiereConnexion(LocalDate date_premiereConnexion) {
this.date_premiereConnexion = date_premiereConnexion;
}
public Long getIdUser() {
return idUser;
}
public void setIdUser(Long idUser) {
this.idUser = idUser;
}
public ProfilUser getProfilUser() {
return profilUser;
}
public void setProfilUser(ProfilUser profilUser) {
this.profilUser = profilUser;
}
public List<ProfilUser> getUserProfils() {
return userProfils;
}
public void setUserProfils(List<ProfilUser> userProfils) {
this.userProfils = userProfils;
}
}
My persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/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_2.xsd">
<!-- Define Persistence Unit -->
<persistence-unit name="g_stock" transaction-type="JTA">
... <!-- Entities -->
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
</properties>
</persistence-unit>
</persistence>
I tried to change the dialect of postgre to 10 (since it is the dialect of version 10 and later) but without success.
I recommend not using the auto generate function of Hibernate as it creates unmaintainable database objects. If you are having issues and you need to manually check stuff in the database you will have trouble figuring out stuff.
I personally would use liquibase or FlywayDB to handle the database migrations. Both are excellent products.
To quote (from point 8 here):
Hibernate can use the mapping information of your entities to generate
a database schema. That’s the easiest approach, and you can see it in
several examples on the internet. That might be OK for a small test
application, but you shouldn’t use it for a business application. The
database schema has a huge influence on the performance and size of
your database. You, therefore, should design and optimize the database
schema yourself and export it as an SQL script. You can run this
script either with an external tool like Flyway or you can use
Hibernate to initialize the database at startup. The following snippet
shows a persistence.xml file which tells Hibernate to run the
create.sql script to setup the database. You can learn more about the
different configuration parameters in Standardized schema generation
and data loading with JPA 2.1.
You probably need to use the new official properties for schema generation.
<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
Or try different settings from this nice overview
Im doing a Spring MVC project and im stucked in this problem.
I have an Entity "TipoDoc", his own Service and Repository using JPA Repository, in my service I have a getAll() method than calls the findAll() JPA Repository method and it works fine, but when I want to use a method to get one by id, im receiving a null object whatever id I send to the method.
So, I started to debug searching the problem and I found a com.sun.jdi.invocationexception in response when Hibernate have to execute the method getOne() from JPA Repository.
I dont know what is wrong in my code, or how can I get more details from the exception.. Im using log4j for loging but i dont know how catch that exception in the log..
Im using MySql Database
Here is my code
#Entity
#Table(name = "TiposDocumento")
public class TipoDoc
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "idTipoDocumento")
private long id;
private String descripcion;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
}
The Service
#Service
public class TipoDocService {
private final TipoDocRepo tipoDocRepo;
#Autowired
public TipoDocService(TipoDocRepo tipoDocRepo) {
this.tipoDocRepo = tipoDocRepo;
}
public List<TipoDoc> getAll() {
return (List<TipoDoc>)tipoDocRepo.findAll();
}
public TipoDoc getById(Long id) {
return (TipoDoc) tipoDocRepo.getOne(id);
}
}
The Repository
public interface TipoDocRepo extends JpaRepository<TipoDoc, Long>{
}
The Controller
#Controller
public class ClientController
{
private static final Logger logger = Logger.getLogger(ClientController.class);
private final ClienteService clienteService;
private final TipoDocService tipoDocService;
private final EstadoCivilService estadoCivilService;
private final ProvinciaService provinciaService;
private final LocalidadService localidadService;
private final CondIvaService condIvaService;
#Autowired
public ClientController(ClienteService clienteService, TipoDocService tipoDocService, EstadoCivilService estadoCivilService,
ProvinciaService provinciaService, LocalidadService localidadService, CondIvaService condIvaService) {
this.clienteService = clienteService;
this.tipoDocService = tipoDocService;
this.estadoCivilService = estadoCivilService;
this.provinciaService = provinciaService;
this.localidadService = localidadService;
this.condIvaService = condIvaService;
}
#RequestMapping("/Clientes")
public ModelAndView formularioCliente()
{
ModelAndView mav = new ModelAndView("clientes");
mav.getModel().put("cliente",new Cliente());
mav.getModel().put("tiposDoc", tipoDocService.getAll()); //Works fine, tiposDoc={{1,DNI};{2,Passaport};{3,LC}}
TipoDoc tipoDoc = tipoDocService.getById((long) 1); //not working tipoDoc={0,null} when it have to be {1,DNI}
mav.getModel().put("estadosCiviles", estadoCivilService.getAll());
mav.getModel().put("provincias", provinciaService.getAll());
mav.getModel().put("localidades", localidadService.getAll());
mav.getModel().put("condicionesIva", condIvaService.getAll());
return mav;
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="OFYS">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/OFYS" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
And this is what can I find debuging
EDIT.
Here is the full description from the exception I can get if u dont see the image
com.sun.jdi.invocationexception: Exception ocurred in target VM ocurred invoking method.
The object does not exist in the database with the given id. Apparently you have a proxy of that object in your persistence context which is returned here. When accessing the object it tries to actually load it from the database and fails because there is no row with that id.
frist of all sorry for my bad english. I am trying to display database (Postgres) rows from one table, and it allways returning null. I am doing project in Java EE using JPA hibernate.
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="mesPU">
<class>pl.mes.model.Users</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="xxxxxx" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
</properties>
</persistence-unit>
</persistence>
Users entity
package pl.mes.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Users {
private Integer userId;
private String firstname;
private String secondname;
private String email;
#Id
#Column(name = "userId", nullable = false)
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
#Basic
#Column(name = "firstname", nullable = true)
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
#Basic
#Column(name = "secondname", nullable = true)
public String getSecondname() {
return secondname;
}
public void setSecondname(String secondname) {
this.secondname = secondname;
}
#Basic
#Column(name = "email", nullable = true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Users users = (Users) o;
if (userId != null ? !userId.equals(users.userId) : users.userId != null) return false;
if (firstname != null ? !firstname.equals(users.firstname) : users.firstname != null) return false;
if (secondname != null ? !secondname.equals(users.secondname) : users.secondname != null) return false;
if (email != null ? !email.equals(users.email) : users.email != null) return false;
return true;
}
#Override
public int hashCode() {
int result = userId != null ? userId.hashCode() : 0;
result = 31 * result + (firstname != null ? firstname.hashCode() : 0);
result = 31 * result + (secondname != null ? secondname.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
}
When i use this
select u from Users u
in console output looks like that:
output
DbSOURCE
I would be grateful if someone can help me with this :)
You have specified the URL jdbc:postgresql://localhost:5432/test for the property javax.persistence.jdbc.url. But when I look at the second screenshot, I can not see a database schema named test.
I guess, you are connecting to the wrong database. Maybe a second instance listening at a different port?
Please also remove the Hibernate specific properties that are already specified by JPA standard properties (as stated out in the comments by Billy Frost).
I have a table called fund, I would like to use JPA in order to write my own queries for it. So, I have use IntelliJ to generate persistence mapping based on my schema and not based on hibernate.
import javax.persistence.*;
import java.sql.Timestamp;
#Entity
#Table(name = "fund", schema = "public", catalog = "db")
public class FundEntity {
private long fundId;
private Timestamp createdAt;
private String description;
private Timestamp modTime;
private String modUser;
private String fundName;
private String fundType;
#Id
#Column(name = "fund_id")
public long getFundId() {
return fundId;
}
public void setFundId(long fundId) {
this.fundId = fundId;
}
#Basic
#Column(name = "created_at")
public Timestamp getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}
#Basic
#Column(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Basic
#Column(name = "mod_time")
public Timestamp getModTime() {
return modTime;
}
public void setModTime(Timestamp modTime) {
this.modTime = modTime;
}
#Basic
#Column(name = "mod_user")
public String getModUser() {
return modUser;
}
public void setModUser(String modUser) {
this.modUser = modUser;
}
#Basic
#Column(name = "fund_name")
public String getFundName() {
return fundName;
}
public void setFundName(String fundName) {
this.fundName = fundName;
}
#Basic
#Column(name = "fund_type")
public String getFundType() {
return fundType;
}
public void setFundType(String fundType) {
this.fundType = fundType;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FundEntity that = (FundEntity) o;
if (fundId != that.fundId) return false;
if (createdAt != null ? !createdAt.equals(that.createdAt) : that.createdAt != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (modTime != null ? !modTime.equals(that.modTime) : that.modTime != null) return false;
if (modUser != null ? !modUser.equals(that.modUser) : that.modUser != null) return false;
if (fundName != null ? !fundName.equals(that.fundName) : that.fundName != null) return false;
if (fundType != null ? !fundType.equals(that.fundType) : that.fundType != null) return false;
return true;
}
#Override
public int hashCode() {
int result = (int) (fundId ^ (fundId >>> 32));
result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (modTime != null ? modTime.hashCode() : 0);
result = 31 * result + (modUser != null ? modUser.hashCode() : 0);
result = 31 * result + (fundName != null ? fundName.hashCode() : 0);
result = 31 * result + (fundType != null ? fundType.hashCode() : 0);
return result;
}
}
And this is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence-unit name="postgres">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5443/db" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="dba" />
<property name="hibernate.connection.password" value="XXX" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
Then I try to fetch my funds but with no results:
jpa-ql> select f from FundEntity f
[2016-06-29 18:01:11] FundEntity is not mapped [select f from FundEntity f]
What am I missing here ? I thought the discovery for my entities would be made automatically since I have specified on my persistence.xml file.
If your #Entity class is not in the same classpath as the persistence.xml file, it will not be automatically loaded. For example -
Is there a way to scan JPA entities not to declare persistent classes in a persistence.xml file?
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 + " ]";
}
}
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>