I'm trying to set up a web application using JBoss and Hibernate, but I can't get the SQL database running, encountering several problems.
My main problem is that when calling persist() Hibernate tries to insert an empty object into my table, the log is starting with this exception:
12:39:26,985 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000228: Running hbm2ddl schema update
12:39:26,986 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000102: Fetching database metadata
12:39:26,987 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000319: Could not get database metadata: java.sql.SQLException: You cannot set autocommit during a managed transaction!
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:888)
After using Google I couldn't really figure out how to solve it, but my application continues running. Somewhere after this Hibernate seems to try to call persist() to a created object.
12:39:27,202 INFO [stdout] (http--127.0.0.1-8080-1) Hibernate:
12:39:27,202 INFO [stdout] (http--127.0.0.1-8080-1) insert
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) into
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) Person
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) (id, birthdate, gender, name, password)
12:39:27,204 INFO [stdout] (http--127.0.0.1-8080-1) values
12:39:27,204 INFO [stdout] (http--127.0.0.1-8080-1) (null, ?, ?, ?, ?)
Using Logger I saw that this Person object is correctly created, but as you can see Hibernate sees the values as (null, ?, ?, ?, ?).
After this this exception is thrown:
12:39:27,218 ERROR [org.apache.tapestry5.ioc.Registry] (http--127.0.0.1-8080-1) org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10031 table: PERSON column: ID
So then, my persistence.xml is:
<?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="facePlace">
<non-jta-data-source>java:jboss/facePlace</non-jta-data-source>
<class>webtech2.faceplace.entities.Person</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
The relevant code is:
#Inject
#Persistence
EntityManager em;
public boolean signUp(String name,
String password,
String repeatPassword,
Date birthdate,
String gender) {
if (!password.equals(repeatPassword)) {
return false;
}
log.info("person data: " + name + " " + password + " " + repeatPassword + " " + birthdate.toString() + " " + gender);
String saltedPassword = hashText + password;
String hashedPassword = generateHash(saltedPassword);
em.getTransaction().begin();
Person xperson = new Person(name, hashedPassword, birthdate, gender);
em.persist(xperson);
em.getTransaction().commit();
return true;
}
My entity looks like:
#Entity
public class Person implements Serializable {
private String name;
private Date birthdate;
private long id;
private String password;
private String gender;
private Set<Person> friends;
#Id
#GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
So I'm not an expert with all this, someone sees some shit in there?
Looks like your primary key is not being set when hibrenate saves the file. the #Id column in your #Entity should specify a primary key generation strategy, rather than leaving #GeneratedValue without any paramaters and having hibernate trying to pick a default generation startegy.
If you are using an identity column in the database you set.
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="pkey")
private Integer pkey;
Related
While learning JPA/Hibernate I stumbbled upon something unexpected. I am getting an unnecessary double select query to the db (see buttom).I have a simple OneToOne setup.
This is my Contact entity:
#Entity
#Table(name = "contact")
public class Contact {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "contact_id", nullable = false, insertable = false, updatable = false)
private Long id;
#Column(name = "name", nullable = false)
private String name;
#OneToOne
#JoinColumn(name="fk_address_id", referencedColumnName="address_id")
private Address address;
public Contact(String name, Address address) {
this.name = name;
this.address = address;
}
// getters/setters
}
My Address entity:
#Entity
#Table(name = "address")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "address_id", nullable = false, insertable = false, updatable = false)
private Long id;
#Column(name = "location", nullable = false)
private String location;
#OneToOne(mappedBy = "address")
private Contact contact;
public Address(String location) {
this.location = location;
}
// getters/setters
}
This is how I ran my code:
private EntityManager em;
#Before
public void setup() {
em = EntityManagerFactoryCreator.getEntityManagerFactory().createEntityManager();
}
#Test
public void createContactWithAddress() {
Address address = new Address("Made");
Contact contact = new Contact("Jan", address);
em.getTransaction().begin();
em.persist(address);
em.persist(contact);
em.getTransaction().commit();
em.close();
em = EntityManagerFactoryCreator.getEntityManagerFactory().createEntityManager();
Contact managedContact = em.find(Contact.class, 1L);
}
}
It is probably something stupid, but what is causing the double select?
Hibernate:
drop table address if exists
Hibernate:
drop table book if exists
Hibernate:
drop table contact if exists
Hibernate:
create table address (
address_id bigint generated by default as identity,
location varchar(255) not null,
primary key (address_id)
)
Hibernate:
create table book (
book_id bigint generated by default as identity,
category varchar(255),
release_date date,
summary varchar(255),
title varchar(255) not null,
primary key (book_id)
)
Hibernate:
create table contact (
contact_id bigint generated by default as identity,
name varchar(255) not null,
address_address_id bigint,
primary key (contact_id)
)
Hibernate:
alter table book
add constraint UK_g0286ag1dlt4473st1ugemd0m unique (title)
Hibernate:
alter table contact
add constraint FKrc0ixa9b11b9tv3hyq0iwvdpt
foreign key (address_address_id)
references address
Hibernate:
insert
into
address
(address_id, location)
values
(null, ?)
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [VARCHAR] - [Made]
Hibernate:
insert
into
contact
(contact_id, address_address_id, name)
values
(null, ?, ?)
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [2] as [VARCHAR] - [Jan]
Hibernate:
select
contact0_.contact_id as contact_1_2_0_,
contact0_.address_address_id as address_3_2_0_,
contact0_.name as name2_2_0_,
address1_.address_id as address_1_0_1_,
address1_.location as location2_0_1_
from
contact contact0_
left outer join
address address1_
on contact0_.address_address_id=address1_.address_id
where
contact0_.contact_id=?
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_1_0_1_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_3_2_0_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([name2_2_0_] : [VARCHAR]) - [Jan]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([location2_0_1_] : [VARCHAR]) - [Made]
Hibernate:
select
contact0_.contact_id as contact_1_2_1_,
contact0_.address_address_id as address_3_2_1_,
contact0_.name as name2_2_1_,
address1_.address_id as address_1_0_0_,
address1_.location as location2_0_0_
from
contact contact0_
left outer join
address address1_
on contact0_.address_address_id=address1_.address_id
where
contact0_.address_address_id=?
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_1_0_0_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([contact_1_2_1_] : [BIGINT]) - [1]
Note:
My EntityManagerFactoryCreator is a singleton that calls
Persistence.createEntityManagerFactory("nl.infosupport.javaminor.week4.jpa.h2");
<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="nl.infosupport.javaminor.week4.jpa.h2">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value="sa"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/Documents/InfoSupport-Minor/h2_embedded_db/test"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
This is normal behavior in this case and with the tables config you have. If you want to have just one select, you need to use a custom query joining the child table. For example, using HQL it would look like that:
public Contact find(Long id) {
TypedQuery<Contact> query = em.createQuery(
"SELECT c FROM Contact c join fetch c.address WHERE c.id = :id", Contact.class);
return query
.setParameter("id", id)
.getSingleResult();
}
The code is not necessary working, I didn't debug it, but it shows the principle.
UPDATED
Or you can try annotating the field like
#Fetch(FetchMode.JOIN)
private Address address;
And then it will fire just one query.
I'm trying to optimize my JPA implementation, using EclipseLink.
I've added batch operations to it. But it is still taking A LOT of time to do 50 000 inserts. It takes more than 10 times the amount of time it takes to do the exact same insert using raw SQL with JDBC.
To make sure batch operations were in fact working I used Wireshark to check my packets and it is not using batch inserts.
Here's one of the insert packets:
It is not doing:
INSERT INTO ENTITYCLASSTEST (LASTNAME, NAME) VALUES ('sfirosijfhgdoi 0', 'dsufius0'), ('sfirosijfhgdoi 0', 'dsufius0'), ('sfirosijfhgdoi 0', 'dsufius0'), ('sfirosijfhgdoi 0', 'dsufius0')... and so on
I was expecting it to do as above but it is inserting one line per packet and not multiple lines per packet.
Here's is my Entity Class:
#Entity
public class EntityClassTest implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String lastname;
public EntityClassTest() {
}
public EntityClassTest(Long id, String name, String lastname) {
this.id = id;
this.name = name;
this.lastname = lastname;
}
public EntityClassTest(String name, String lastname) {
this.name = name;
this.lastname = lastname;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getLastName() {
return lastname;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
#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 EntityClassTest)) {
return false;
}
EntityClassTest other = (EntityClassTest) 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 "database.EntityClassTest [id=" + id + " ]";
}
}
And here is my persist method that receives a List and persists all the objects inside.
public void insertListToTable(final String persistenceUnit, final List list) throws SQLException {
final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
final EntityManager entityManager = entityManagerFactory.createEntityManager();
final EntityTransaction transaction = entityManager.getTransaction();
try {
final int listSize = list.size();
transaction.begin();
for (int i = 0; i<listSize; i++) { //Object object : list) {
final Object object = list.get(i);
entityManager.persist(object);
if ( i % 500 == 0 ) { //500, same as the JDBC batch size defined in the persistence.xml
//flush a batch of inserts and release memory:
entityManager.flush();
entityManager.clear();
}
}
transaction.commit();
}
catch(Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new SQLException(e.getMessage());
}
finally {
entityManager.close();
}
}
And my persistence.xml, where I set 500 as the batch value, file is:
<?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="ExternalServer" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- List of Entity classes -->
<class>model.EntityClassTest</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://myServer:3306/testdb?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="testdbuser"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="myPassword"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<!-- Weaving -->
<property name="eclipselink.weaving" value="static"/>
<!-- SQL dialect / Database type -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="eclipselink.target-database" value="MySQL"/>
<!-- Tell the JPA provider to, by default, create the table if it does not exist. -->
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<!-- No logging (For development change the value to "FINE") -->
<property name="eclipselink.logging.level" value="OFF"/>
<!-- Enable batch writing -->
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<!-- Batch size -->
<property name="eclipselink.jdbc.batch-writing.size" value="500"/>
</properties>
</persistence-unit>
</persistence>
So my question is, why is it not batch inserting? I believe I've EclipseLink well configured to do so from what I've been reading around on EclipseLink website and here SO as well.
//////////////////////////// EDIT //////////////////////////
As suggested by Chris's answer, I changed in my EntityClassTest this value #GeneratedValue(strategy = GenerationType.IDENTITY) to #GeneratedValue(strategy = GenerationType.SEQUENCE) and re-run the test and the packets are being sent as before (like the image I posted above). So it didn't fix my problem I'm afraid.
//////////////////////////// EDIT 2 ////////////////////////
I've changed the logging level in the persistence.xml file to FINEST as shown next.
<property name="eclipselink.logging.level" value="FINEST"/>
And here is the log generated. I placed it in a pastebin because it is quite long.
http://pastebin.com/rKihCKMW
It seems to be calling Execute query InsertObjectQuery quite a lot of times.
//////////////////////////// EDIT 3 ////////////////////////
Here's the version for each component I'm using.
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.12 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.12-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
+-------------------------+------------------------------+
Netbeans 8.0
EclipseLink (JPA 2.1)
mysql-connector-java-5.1.24.jar
//////////////////////////// EDIT 4 ////////////////////////
Following CuriousMind's answer I've edited my EntityClassTest id annotation to:
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator="id-seq-gen")
#SequenceGenerator( name="id-seq-gen", sequenceName="ID_SEQ_GEN", allocationSize=500 )
private Long id;
But it didn't solve my problem, I'm still getting one single insert per packet (as described by the image above) and on the EclipseLink log I'm getting:
[EL Fine]: sql: 2014-10-19 06:44:02.608--ClientSession(824177287)--Connection(1674390738)--Thread(Thread[main,5,main])--SELECT LAST_INSERT_ID()
[EL Finest]: sequencing: 2014-10-19 06:44:02.608--UnitOfWork(1985011414)--Thread(Thread[main,5,main])--assign sequence to the object (1.251 -> database.EntityClassTest [id=null ])
[EL Finest]: query: 2014-10-19 06:44:02.608--UnitOfWork(1985011414)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(database.EntityClassTest [id=null ])
[EL Finest]: query: 2014-10-19 06:44:02.608--ClientSession(824177287)--Thread(Thread[main,5,main])--Execute query ValueReadQuery(name="ID_SEQ_GEN" sql="SELECT LAST_INSERT_ID()")
[EL Fine]: sql: 2014-10-19 06:44:02.608--ClientSession(824177287)--Connection(1674390738)--Thread(Thread[main,5,main])--INSERT INTO ENTITYCLASSTEST (LASTNAME, NAME) VALUES (?, ?)
bind => [sfirosijfhgdoi 2068, dsufius1034]
[EL Fine]: sql: 2014-10-19 06:44:02.608--ClientSession(824177287)--Connection(1674390738)--Thread(Thread[main,5,main])--SELECT LAST_INSERT_ID()
[EL Finest]: sequencing: 2014-10-19 06:44:02.608--UnitOfWork(1985011414)--Thread(Thread[main,5,main])--assign sequence to the object (1.252 -> database.EntityClassTest [id=null ])
[EL Finest]: query: 2014-10-19 06:44:02.608--UnitOfWork(1985011414)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(database.EntityClassTest [id=null ])
[EL Finest]: query: 2014-10-19 06:44:02.608--ClientSession(824177287)--Thread(Thread[main,5,main])--Execute query ValueReadQuery(name="ID_SEQ_GEN" sql="SELECT LAST_INSERT_ID()")
[EL Fine]: sql: 2014-10-19 06:44:02.608--ClientSession(824177287)--Connection(1674390738)--Thread(Thread[main,5,main])--INSERT INTO ENTITYCLASSTEST (LASTNAME, NAME) VALUES (?, ?)
bind => [sfirosijfhgdoi 2244, dsufius1122]
And so on...
You are using GenerationType.IDENTITY for sequencing, which requires retrieving the IDs from each insert statement one by one. Try a sequencing scheme that allows preallocation in batches of 500 and you will see improvements:
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Identity_sequencing
It's been one year already, probably it's too late to answer. In my case, I found that flush() cause the problem. I call flush() after persist() for each record. This prevent batch writing to make an optimization and results in poor insert performance. After removing flush(), everything went well.
It seems the sequence generation has been causing the issue, you refer to this post. It provides approach of preallocated sequence generation.
This could be becuase of the sequence/indentity (need to use preallocation), but also for mysql I think you need a connection string property to allow bulk insert:
rewriteBatchedStatements=true
jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
The entity class is :
#Entity
#Table(name = "movieDetail")
public class MovieDetailImpl implements MovieDetail {
#Id
// primary key
#Column(name = "idmovieDetail")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "cast")
private String cast;
#Column(name = "producer")
private String producer;
#Column(name = "director")
private String director;
#Column(name = "trailer")
private URL trailer;
#Column(name = "photo")
private URL photo;
#Column(name = "plot")
private URL plot;
#Column(name = "desc")
private String desc;
#Column(name = "moreDetails")
private URL moreDetails;
// Getters/Setters
}
I am trying to persist a MovieDetail entity with only cast set. Rest of the fields are null.
Hibernate throws the below exception:
014-08-17 21:47:35 INFO TransactionFactoryInitiator:62 - HHH000399:
Using default transaction strategy (direct JDBC transactions)
2014-08-17 21:47:35 INFO ASTQueryTranslatorFactory:47 - HHH000397:
Using ASTQueryTranslatorFactory
2014-08-17 21:47:36 INFO HibernateTransactionManager:341 - Using
DataSource
[org.springframework.jdbc.datasource.DriverManagerDataSource#1fba434a]
of Hibernate SessionFactory for HibernateTransactionManager
Hibernate: insert into movieDetail (cast, desc, director, moreDetails,
photo, plot, producer, trailer) values (?, ?, ?, ?, ?, ?, ?, ?)
2014-08-17 21:47:36 WARN SqlExceptionHelper:144 - SQL Error: 1064,
SQLState: 42000
2014-08-17 21:47:36 ERROR SqlExceptionHelper:146 - You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'desc, director,
moreDetails, photo, plot, producer, trailer) values ('testGetAll' at
line 1
2014-08-17 21:47:36 INFO GenericApplicationContext:873 - Closing
org.springframework.context.support.GenericApplicationContext#40266966:
startup date [Sun Aug 17 21:47:34 PDT 2014]; root of context hierarchy
In my 'database.properties' , I have :
jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
I am using a MySQL Community Server 5.6.20. and the mysql java driver version dependency defined in maven's pom.xml is
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
Am I missing anything here?
You have a column named desc, and desc is a reserved work in MySql (and many other databases).
You can:
From this and this you can make hibernate escape all column and table names: hibernate.globally_quoted_identifiers=true (in your persistence.xml, or hibernate configuration.
Change the name of the column
From this you can escape only this identifier using:
#Column(name="\"desc\"")
Also, if is not a legacy database, please consider renaming.
I want to create list of embedded objects with Hibernate and MySql.
But I cought bunch of errors:
Hibernate: alter table USERS drop foreign key FK_qymdwjo8d0eu0lhfd3ngfs74d
2014-07-09 15:40:47 ERROR SchemaExport:425 - HHH000389: Unsuccessful: alter table USERS drop foreign key FK_qymdwjo8d0eu0lhfd3ngfs74d
2014-07-09 15:40:47 ERROR SchemaExport:426 - Can't DROP 'FK_qymdwjo8d0eu0lhfd3ngfs74d'; check that column/key exists
Hibernate: drop table if exists USERS
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: drop table if exists USERS
2014-07-09 15:40:48 ERROR SchemaExport:426 - Cannot delete or update a parent row: a foreign key constraint fails
Hibernate: drop table if exists hibernate_unique_key
Hibernate: create table USERS (id integer not null, city varchar(35), pincode varchar(35), state varchar(35), street varchar(35), description varchar(35), joinedDate date, name varchar(35), ADDRESSES_ID bigint not null, primary key (ADDRESSES_ID))
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: create table USERS (id integer not null, city varchar(35), pincode varchar(35), state varchar(35), street varchar(35), description varchar(35), joinedDate date, name varchar(35), ADDRESSES_ID bigint not null, primary key (ADDRESSES_ID))
2014-07-09 15:40:48 ERROR SchemaExport:426 - Table 'users' already exists
Hibernate: alter table USERS add constraint FK_qymdwjo8d0eu0lhfd3ngfs74d foreign key (id) references USERS (ADDRESSES_ID)
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: alter table USERS add constraint FK_qymdwjo8d0eu0lhfd3ngfs74d foreign key (id) references USERS (ADDRESSES_ID)
2014-07-09 15:40:48 ERROR SchemaExport:426 - Cannot add foreign key constraint
Hibernate: create table hibernate_unique_key ( next_hi integer )
Hibernate: insert into hibernate_unique_key values ( 0 )
2014-07-09 15:40:48 INFO SchemaExport:405 - HHH000230: Schema export complete
Hibernate: insert into USERS (city, pincode, state, street, description, joinedDate, name) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select next_hi from hibernate_unique_key for update
Hibernate: update hibernate_unique_key set next_hi = ? where next_hi = ?
Hibernate: insert into USERS (id, ADDRESSES_ID, city, pincode, state, street) values (?, ?, ?, ?, ?, ?)
2014-07-09 15:40:48 WARN SqlExceptionHelper:144 - SQL Error: 1054, SQLState: 42S22
2014-07-09 15:40:48 ERROR SqlExceptionHelper:146 - Unknown column 'ADDRESSES_ID' in 'field list'
2014-07-09 15:40:48 INFO AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.demo.hibernate.HibernateDemo.createUser(HibernateDemo.java:59)
at com.demo.hibernate.HibernateDemo.main(HibernateDemo.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'ADDRESSES_ID' in 'field list'
Here is main():
public static void main(String[] args) {
try {
HibernateDemo demo = new HibernateDemo();
UserDetails user = new UserDetails();
Address address = new Address();
address.setStreet("Name");
address.setCity("Kiev");
address.setPincode("00000");
address.setState("My state");
Address addr = new Address();
addr.setStreet("new name");
addr.setCity("Lviv");
addr.setPincode("79040");
addr.setState("state");
user.getListOfAddresses().add(address);
user.getListOfAddresses().add(addr);
user.setUserName("Carl");
user.setJoinedDate(new Date());
user.setDescription("it is cool guy");
user.setAddress(address);
demo.createUser(user);
demo.listUsers();
user.setUserName("Bruno Shults");
demo.updateUser(user);
demo.listUsers();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.runFinalizersOnExit(true);
System.exit(1);
}
UserDetails class:
#Entity
#Table(name = "USERS")
public class UserDetails {
#Id
#GeneratedValue
#Column(name = "id")
private int userId;
#Column(name = "name", length = 35)
private String userName;
#Temporal(TemporalType.DATE)
private Date joinedDate;
#Column(length = 35)
private Address address;
#Column(length = 35)
private String description;
#ElementCollection
#JoinTable(name = "USERS", joinColumns = #JoinColumn(name = "id"))
#GenericGenerator(name = "hilo-gen", strategy = "hilo")
#CollectionId(columns = {#Column(name = "ADDRESSES_ID")}, generator = "hilo-gen", type = #Type(type = "long"))
private Collection<Address> listOfAddresses = new ArrayList<Address>();
// getters and setters
Address class:
#Embeddable
public class Address {
#Column(length = 35)
private String street;
#Column(length = 35)
private String city;
#Column(length = 35)
private String state;
#Column(length = 35)
private String pincode;
// getters and setters
cfg xml file:
<hibernate-configuration>
<session-factory>
<!--Database connection settings-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">secret</property>
<!--JDBC connection pool-->
<property name="hibernate.connection.pool_size">2</property>
<!--SQL dialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Disable the second level cache-->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
<!--Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!--Drop and recreate the database schema on startup-->
<property name="hbm2ddl.auto">create</property>
<!--<property name="hbm2ddl.auto">update</property>-->
<!-- List of XML mapping files -->
<mapping class="com.demo.dto.UserDetails"/>
Here is DB struckture looking:
and diagram:
I couldn't figure out why it creates hibernate_unique_key table? It shouldn't.
Any suggestions?
well these lines
Hibernate: create table hibernate_unique_key ( next_hi integer )
Hibernate: insert into hibernate_unique_key values ( 0 )
appear because of
#GenericGenerator(name = "hilo-gen", strategy = "hilo")
in your address
hilo: Generates a Integer, long or short type of ids. This uses a High – Low algorithm. As the name suggests, it depends upon the highest table id and then reads the possible lowest available value.
Read more about it here
I am trying to make a add to wish list feature for my app but I keep getting this error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'WISH_ID' cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO WISHLIST (BOOK_TITLE, CUSTOMER_ID) VALUES (?, ?)
bind => [2 parameters bound]
Query: InsertObjectQuery(dukesbookstore.entity.Wishlist[ wishId=null ])
ENTITY class:
#Entity
#Table(name = "WISHLIST")
#XmlRootElement
#NamedQueries(
{
#NamedQuery(name = "Wishlist.findByCustomerId", query = "SELECT w FROM Wishlist w WHERE w.customerId = :customerId"),
})
public class Wishlist implements Serializable
{
private static final long serialVersionUID = 1L;
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "WISH_ID")
private Integer wishId;
#Column(name = "CUSTOMER_ID")
private Integer customerId;
#Size(max = 35)
#Column(name = "BOOK_TITLE")
private String bookTitle;
public Wishlist()
{
}
public Integer getCustomerId()
{
return customerId;
}
public void setCustomerId(Integer customerId)
{
this.customerId = customerId;
}
public String getBookTitle()
{
return bookTitle;
}
public void setBookTitle(String bookTitle)
{
this.bookTitle = bookTitle;
}
}
and this is the code for creating a new wish:
public void createWishlist(String title,int cust_id)
{
Wishlist newWish = new Wishlist();
newWish.setBookTitle(title);
newWish.setCustomerId(cust_id);
em.persist(newWish);
}
I tried to look at other similar problems but they involves hibernate which i am not using. I have also tried various generation strategy such as AUTO,SEQUENCE,TABLE but all failed. I also have another entity named customer which is exactly same but it works fine though its created from a form.
Changing to AUTO generates this error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SEQUENCE' does not exist.
Error Code: -1
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
root cause
java.sql.SQLSyntaxErrorException: Table/View 'SEQUENCE' does not exist.
root cause
org.apache.derby.client.am.SqlException: Table/View 'SEQUENCE' does not exist.
Persistence.xml incase relevant
<?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="myStorePU" transaction-type="JTA">
<jta-data-source>mydb</jta-data-source>
<class>myStore.entity.Book</class>
<class>myStore.entity.Customer</class>
<class>myStore.entity.Wishlist</class>
<properties>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
Finally it is working now,
firstly, i had to delete the table that I created from netbeans db creation tool, which had a creation code like this, as seen by using "grab structure"
create table "APP".WISHLIST
(
WISH_ID NUMERIC(5) not null primary key,
CUSTOMER_ID NUMERIC(5),
BOOK_TITLE VARCHAR(100)
)
secondly, I added this code into my persistence.xml file
<property name="eclipselink.ddl-generation" value="create-tables"/>
This solved the problem as it created the table by it self, which have different creation code, as seen from its grab structure from auto creation:
create table "APP".WISHLIST
(
WISH_ID INTEGER default GENERATED_BY_DEFAULT not null primary key,
CUSTOMER_ID NUMERIC(5),
BOOK_TITLE VARCHAR(100)
)
So, basically should let netbeans create the table itself from entity but i was using "Create entity from table" features, for that i had to create the tables first in netbeans gui.
Thank you #Geziefer for all the help, I learned quite a bit from your help too.
This might be a "Non nullable attributes" in JPA single-table-inheritance problem. It might help and doesn't hurt to specify
#JoinTable(name = "[some join table name]",
joinColumns = {#JoinColumn(name = "[some ID column name]")},
inverseJoinColumns = {#JoinColumn(name = "[some different ID column name]")})