I'm trying to convert a simple application-managed entity manager program from EclipseLink to Hibernate but I some reasons I'm getting the following errors, if anyone can give me some suggestions that would be great!
Code:
Oct 5, 2011 8:42:37 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.0.CR2}
Oct 5, 2011 8:42:37 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.0.CR4}
Oct 5, 2011 8:42:37 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 5, 2011 8:42:37 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 5, 2011 8:42:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Oct 5, 2011 8:42:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Oct 5, 2011 8:42:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
Oct 5, 2011 8:42:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/Music]
Oct 5, 2011 8:42:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true, release_mode=auto}
Oct 5, 2011 8:42:38 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Oct 5, 2011 8:42:38 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Oct 5, 2011 8:42:38 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: Demo] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:916)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:887)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at ModifyArtistClient.main(ModifyArtistClient.java:8)
Caused by: org.hibernate.MappingException: Could not instantiate id generator [entity-name=Song]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:190)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:305)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1723)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:907)
... 5 more
Caused by: org.hibernate.MappingException: org.hibernate.dialect.MySQLDialect does not support sequences
at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:789)
at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:99)
at org.hibernate.id.SequenceHiLoGenerator.configure(SequenceHiLoGenerator.java:58)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118)
... 10 more
persistence.xml file
Code:
<?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="Demo" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Artist</class>
<class>Album</class>
<class>Song</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/Music"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="12345"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Driver File:
Code:
import java.sql.Date;
import javax.persistence.*;
public class ModifyArtistClient{
public static void main(String [] args){
//New entity manager factory for modifying artist table only (static method), specified persistence unit name
EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("Demo");
//New entity manager obtained from factory
EntityManager em1 = emf1.createEntityManager();
//Entity manager obtained to modify entity
ModifyArtist m1 = new ModifyArtistBean(em1);
//Tests
try{
//Adds to table
em1.getTransaction().begin();
m1.addArtist("Missy Elliot", "Altantic", "USA", Date.valueOf("1970-07-01"));
m1.addArtist("Tinie Tempah", "Parlophone", "UK", Date.valueOf("1988-11-07"));
m1.addArtist("Craig David", "Universal", "UK", Date.valueOf("1981-05-05"));
m1.addArtist("Jay Sean", "Young Money", "UK", Date.valueOf("1981-03-26"));
m1.addArtist("Mario", "J Records", "USA", Date.valueOf("1986-08-27"));
em1.getTransaction().commit();
System.out.println("Added artists successfully");
//remove entry with specified parameters
em1.getTransaction().begin();
int id = m1.findArtist("Mario", Date.valueOf("1986-08-27"));
m1.removeArtist(id);
em1.getTransaction().commit();
System.out.println("Has removed artist");
//print out remaining rows in tables
System.out.println("Printing all artists in database: ");
m1.print();
}
catch(PersistenceException e){
e.printStackTrace();
}
finally{
//close entity manager
em1.close();
//close entity manager factory
emf1.close();
}
}
}
Implementation:
Code:
//For modifying rows in Artist table
import javax.persistence.*;
import java.util.Date;
import java.util.List;
public class ModifyArtistBean implements ModifyArtist{
//Query string for finding id based on Artist name and birth date which is unique
private String idQuery = "Select a.artistID " + "From Artist a " + "Where a.name =:artistName AND " + "a.dateOfBirth = :artistBirthday ";
//Entity Manager
@PersistenceContext(unitName="DemoJPA")
private EntityManager em;
//Constructor, associates with an entity manager (needed for creating, reading, writing to an entity)
public ModifyArtistBean(EntityManager em){
this.em = em;
}
//Adds new artist to table with specified parameters
public void addArtist(String name, String currentRecordCompany, String countryOfOrigin, Date birthDate){
Artist artist = new Artist(name, currentRecordCompany, countryOfOrigin, birthDate);
//persist artist to database
em.persist(artist);
}
//Remove artist with given ID
public void removeArtist(int artistID) throws IllegalArgumentException{
try{
//find artist with ID
Artist artist = em.find(Artist.class, artistID);
if(artist != null){
em.remove(artist);
}
}
catch(IllegalArgumentException e){
System.out.println("Incorrect artist ID");
}
}
//Changes entire row to values specified
public void modifyRow(int artistID, String name, String currentRecordCompany, String countryOfOrigin, Date birthDate) throws IllegalArgumentException{
try{
//find artist with ID, changes all values in row to values specified
Artist artist = em.find(Artist.class, artistID);
if(artist != null){
artist.setName(name);
artist.setCurrentRecordCompany(currentRecordCompany);
artist.setCountryOfOrigin(countryOfOrigin);
artist.setDateOfBirth(birthDate);
}
}
catch(IllegalArgumentException e){
System.out.println("Incorrect artist ID");
}
}
//Returns id of artist with specified name and birthdate
public int findArtist(String name, Date birthdate)
{
Query q1 = em.createQuery(idQuery);
q1.setParameter("artistName", name);
q1.setParameter("artistBirthday", birthdate);
return (Integer) q1.getSingleResult();
}
//Returns query in List of type Artist
public List<Artist> returnAll(){
TypedQuery<Artist> queryall = (TypedQuery<Artist>) em.createQuery("SELECT a FROM Artist a");
return queryall.getResultList();
}
//Print all rows in table
public void print(){
List<Artist> resultList = this.returnAll();
for(Artist a: resultList){
System.out.println("Artist: ");
System.out.println("ArtistID = " + a.getArtistID());
System.out.println("Artist Name = " + a.getName());
System.out.println("Record Company = " + a.getCurrentRecordCompany());
System.out.println("Country = " + a.getCountryOfOrigin());
System.out.println("Birth Date = " + a.getDateOfBirth());
}
}
}
Entity class:
Code:
import java.util.Date;
import javax.persistence.*;
@Entity
public class Artist{
//Fields
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE) private int artistID;
private String name;
private String currentRecordCompany;
private String countryOfOrigin;
//Use @Temporal for Date, Time variables
@Temporal(TemporalType.DATE) java.util.Date dateOfBirth;
//Default constructor
public Artist(){
}
//Constructor
public Artist(String artistName, String currentRecordCompany, String countryOfOrigin, Date dateOfBirth){
this.name = artistName;
this.currentRecordCompany = currentRecordCompany;
this.countryOfOrigin = countryOfOrigin;
this.dateOfBirth = dateOfBirth;
}
//Returns artist name
public String getName() {
return name;
}
//Edits artist name
public void setName(String name) {
this.name = name;
}
//Returns artist record label
public String getCurrentRecordCompany() {
return currentRecordCompany;
}
//Edits artist record label
public void setCurrentRecordCompany(String currentRecordCompany) {
this.currentRecordCompany = currentRecordCompany;
}
//Returns artist origin
public String getCountryOfOrigin() {
return countryOfOrigin;
}
//Edits artist origin
public void setCountryOfOrigin(String countryOfOrigin) {
this.countryOfOrigin = countryOfOrigin;
}
//Returns artist birth date
public Date getDateOfBirth() {
return dateOfBirth;
}
//Edits artist birth date
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
//Returns artistID (primary key for database)
public int getArtistID() {
return artistID;
}
}
Jars In class path:
antlr-2.7.7.jar
classmate-0.5.4.jar
commons-collection-3.2.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.0.CR2.jar
hibernate-core-4.0.0.CR4.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jandex-1.0.3.Final.jar
javaassist-3.12.1.GA.jar
jboss-logging-3.0.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.0.0.CR4.jar