JPA/Hibernate double select on OneToOne Find - java

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.

Related

Hibernate not set foreign key

I have this model
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#OneToOne
#PrimaryKeyJoinColumn
private Address registred_address_id;
#OneToOne
#PrimaryKeyJoinColumn
private Address actual_address_id;
...
And I have this method
private Customer addCustomer(String first_name, String last_name, String middle_name, String sex, Address address) {
Customer customer = new Customer(first_name, last_name, middle_name, sex);
customer.setActual_address_id(address);
customer.setRegistered_address_id(address);
customerRepo.save(customer);
return customer;
}
But hibernate not set actual_address_id and registered_address_id (it,s OneToOne)
Hibernate:
insert
into
customer
(first_name, last_name, middle_name, sex)
values
(?, ?, ?, ?)
2021-03-18 14:01:58.340 WARN 12836 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23502
2021-03-18 14:01:58.340 ERROR 12836 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ОШИБКА: значение NULL в столбце "registred_address_id" отношения "customer" нарушает ограничение NOT NULL
Подробности: Ошибочная строка содержит (6, null, null, null, null, null, male).
You mapping is contradictory.
From one side you want Customer.id to be generated by database after its insert. This is what GenerationType.IDENTITY actually mean.
From another side you try to use #PrimaryKeyJoinColumn what mean Customer.id should be set by Address.id. The correct mapping for #PrimaryKeyJoinColumn should look something like this:
#Entity
public class Customer {
#Id
private long id;
#OneToOne
#PrimaryKeyJoinColumn
private Address address;
public void setAddress(Address address) {
this.address = address;
this.id = address.getId();
}
// ...
}
but this will not fit your requirements.
So, I would suggest you correct your customer table like this:
create table customer
(
id bigserial not null,
reg_address_id bigint,
act_address_id bigint,
-- ...
primary key(id),
foreign key(reg_address_id) references address(id),
foreign key(act_address_id) references address(id)
);
and then use the following mapping:
#Entity
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToOne
#JoinColumn(name = "reg_address_id")
private Address registredAddress;
#OneToOne
#JoinColumn(name = "act_address_id")
private Address actualAddress;
// ...
}

Hibernate left join fetch to soft-deleted entity mapped with #ManyToMany association generates invalid query?

I don't know if this is a problem with Hiberante or if I'm doing something wrong.
I tried to make this example as simple as possible.
My entities:
Book.java:
#Entity
#Table(name="books")
#SQLDelete(sql="UPDATE books set is_deleted = true where id = ?")
#Where(clause="is_deleted = false")
public class Book implements Serializable {
#Id
#Column(name = "id", updatable = false, nullable = false)
private long id;
#Column (name="is_deleted")
private boolean deleted;
#Column(name="title")
private String title;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(
name="books_authors",
joinColumns = {
#JoinColumn(name="id_books")
},
inverseJoinColumns = {
#JoinColumn(name="id_authors")
}
)
private Set<Author> authors = new HashSet<>();
//getters/setters
}
Author.java:
#Entity
#Table(name="autors")
#SQLDelete(sql="UPDATE autors set is_deleted = true where id = ?")
#Where(clause="is_deleted = false")
public class Author implements Serializable {
#Id
#Column(name = "id", updatable = false, nullable = false)
private long id;
#Column(name="name")
private String name;
#Column (name="is_deleted")
private boolean deleted;
#ManyToMany(mappedBy = "authors", fetch = FetchType.LAZY)
private Set<Book> books = new HashSet<>();
//getters/setters
}
underlying tables:
create table autors (
id int8 not null,
is_deleted boolean,
name varchar(255),
primary key (id)
);
create table books (
id int8 not null,
is_deleted boolean,
title varchar(255),
primary key (id)
);
create table books_authors (
id_books int8 not null,
id_authors int8 not null,
primary key (id_books, id_authors)
);
alter table if exists books_authors add constraint FK9xru8ocoxufr5eva6atfyfjxh foreign key (id_authors) references autors;
alter table if exists books_authors add constraint FKx5d5025t9ct5kcbwwxm6tipo foreign key (id_books) references books;
Data:
select * from books;
id
is_deleted
title
1
false
TITLE_1
2
false
TITLE_2
select * from authors;
id
is_deleted
name
1
false
not deleted
2
true
DELETED!
select * from books b
join books_authors ba on b.id = ba.id_books
join autors a on a.id = ba.id_authors;
id
is_deleted
title
id_books
id_authors
id
is_deleted
name
1
false
TITLE_1
1
1
1
false
not deleted
2
false
TITLE_2
2
2
2
true
DELETED!
JPQL:
public Optional<Book> getBookWithSoftDeletedAuthor(long bookId) {
List<Book> myBooks = em.createQuery(
"select b from " +
"Book b " +
"left join fetch b.authors a " +
"where b.id = :bookId", Book.class)
.setParameter("bookId", bookId)
.getResultList();
return myBooks.size() > 0 ? Optional.of(myBooks.get(0)) : Optional.empty();
}
persistence.xml:
<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="WildflyUnit">
<description> Persistence unit for Wildfly </description>
<jta-data-source>java:jboss/datasources/WildflyDataSource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
</properties>
</persistence-unit>
</persistence>
datasource from Wildfly's standalone.xml:
<datasource jndi-name="java:jboss/datasources/WildflyDataSource" pool-name="WildflyDataSource" enabled="true" use-java-context="true" statistics-enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/TestDB</connection-url>
<driver>postgresql</driver>
<security>
<user-name>wildfly_user</user-name>
<password>********</password>
</security>
</datasource>
Hibernate generates sql statement like this:
select
book0_.id as id1_1_0_,
author2_.id as id1_0_1_,
book0_.is_deleted as is_delet2_1_0_,
book0_.title as title3_1_0_,
author2_.is_deleted as is_delet2_0_1_,
author2_.name as name3_0_1_,
authors1_.id_books as id_books1_2_0__,
authors1_.id_authors as id_autho2_2_0__ -- this column is not null even if the author is soft-deleted
from
books book0_
left outer join
books_authors authors1_
on book0_.id=authors1_.id_books
left outer join
authors author2_
on authors1_.id_authors=author2_.id
and (
author2_.is_deleted = false
)
where
(
book0_.is_deleted = false
)
and book0_.id=2
imho Hibernate should generate this query like this:
select
book0_.id as id1_1_0_,
author2_.id as id1_0_1_,
book0_.is_deleted as is_delet2_1_0_,
book0_.title as title3_1_0_,
author2_.is_deleted as is_delet2_0_1_,
author2_.name as name3_0_1_,
book0_.id as id_books1_2_0__,
author2_.id as id_autho2_2_0__
from
books book0_
left outer join
books_authors authors1_
on book0_.id=authors1_.id_books
left outer join
authors author2_
on authors1_.id_authors=author2_.id
and (
author2_.is_deleted = false
)
where
(
book0_.is_deleted = false
)
and book0_.id=2
the id_autho2_2_0__ column should come from author2_ (authors table), not from authors1_ (books_authors table). Since the value of the authors1_.id_authors column is not null, hibernate tries to fetch a soft deleted entity from the authors table.
I use Hibernate 5.4.27.Final

JPA/Hibernate: ManyToMany delete relation on Join Table

Disclaimer: This is my first Java project; learning as I go.
Background: I've inherited a legacy database on which to build a new RESTful API. We're using Elide with Spring Boot to provide a JSON API compliant service.
Reference: Example source code
Problem: We have entities with a many-to-many relationship to each other and themselves by way of a join table. Consider the followig schema:
CREATE TABLE ALPHA (
ID VARCHAR(255),
NAME VARCHAR(255),
CONSTRAINT PK_ALPHA PRIMARY KEY (ID)
);
CREATE TABLE BRAVO (
ID VARCHAR(255),
NAME VARCHAR(255),
CONSTRAINT PK_BRAVO PRIMARY KEY (ID)
);
CREATE TABLE RELATIONSHIP (
ID INT AUTO_INCREMENT,
FROM_ID VARCHAR(255),
TO_ID VARCHAR(255)
);
Where the resource entities are modeled as follows:
public class Alpha implements Serializable {
private String id;
private String name;
private Set<Alpha> alphas = new HashSet<>();
private Set<Bravo> bravos = new HashSet<>();
#Id
#Column(name = "ID", unique = true, nullable = false)
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
}
public String getName() {
return name;
}
#ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(
name = "RELATIONSHIP",
joinColumns = #JoinColumn(name = "FROM_ID"),
inverseJoinColumns = #JoinColumn(name = "TO_ID")
)
public Set<Alpha> getAlphas() {
return alphas;
}
#ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(
name = "RELATIONSHIP",
joinColumns = #JoinColumn(name = "FROM_ID"),
inverseJoinColumns = #JoinColumn(name = "TO_ID")
)
public Set<Bravo> getBravos() {
return bravos;
}
}
And the relationship table:
public class Relationship implements Serializable {
private Integer id;
private String fromId;
private String toId;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
#Column(name = "FROM_ID")
public String getFromId() {
return fromId;
}
#Column(name = "TO_ID")
public String getToId() {
return toId;
}
}
Now let's say we have an Alpha record A1 with relationships to A2, A3, B1, and B2. First we delete the relationship to A2.
From our API this would be a DELETE request to http://localhost:9000/api/alphas/a1/relationships/alphas with BODY
{
"data": [
{
"type": "alphas",
"id": "a2"
}
]
}
Behind the scenes Hibernates does what I'm expecting and generates the following SQL queries:
2018-07-13 09:48:23.687 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alpha0_.id as id1_0_,
alpha0_.name as name2_0_
from
alpha alpha0_
where
alpha0_.id in (
?
)
2018-07-13 09:48:23.688 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.690 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alphas0_.from_id as from_id2_2_0_,
alphas0_.to_id as to_id3_2_0_,
alpha1_.id as id1_0_1_,
alpha1_.name as name2_0_1_
from
relationship alphas0_
inner join
alpha alpha1_
on alphas0_.to_id=alpha1_.id
where
alphas0_.from_id=?
2018-07-13 09:48:23.690 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.699 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alpha0_.id as id1_0_,
alpha0_.name as name2_0_
from
alpha alpha0_
where
alpha0_.id in (
?
)
2018-07-13 09:48:23.699 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a2]
2018-07-13 09:48:23.721 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
delete
from
relationship
where
from_id=?
and to_id=?
2018-07-13 09:48:23.722 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.724 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [a2]
The key piece being delete from relationship where from_id=? and to_id=?
Now the problem arises when trying to delete the second Alpha relationship A3, in which Hibernate does almost the exact same sequence, except for the DELETE query which omits the and to_id=? from the query, i.e.
Hibernate:
delete
from
relationship
where
from_id=?
Which has the unintended consequence of deleting all other A1 relationships in the table, i.e. B1 and B2.
So that is the crux of my problem. It seems like Hibernate is only seeing one other related Alpha record and therefore deciding to simplify the query by omitting the and to_id statement.
I'm probably missing something terribly obvious!
I should also point out that I attempted to use a composite key on the relationship table but to no avail.
This is an unusual design, which I suspect is confusing Hibernate. Sharing a single join table between multiple Many-to-many relationships, isn't good database design, for one it can't have any foreign keys/referential integrity.
Secondly, Hibernate manages relationships, and therefore has control over the #JoinTable, I don't know how it would handle multiple entity relationships mapped with the same table. Evidently, not very well though!
The simplest solution (if you're able to), would be to have 2 mapping tables. One for the relationship between Alpha-Alpha and another between Alpha-Bravo.

ERROR SchemaExport with Hibernate

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

hibernate: objects created in wrong order?

I just switched from XML mapping to annotations and had to realize that my serialization class does not work any more!
I hope you can help me to find out the reason :)
I have a School class that contains an Address
#Entity
#Table(name="schools")
public class School {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "id")
private Long id;
#ManyToOne
#JoinColumn(name="address_id", nullable=true)
private Address address;
...
}
the Address class:
#Entity
#Table(name="addresses")
public class Address {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Column(name="street", nullable=false, updatable=true)
private String street; // with nr
...
}
I try to serialize like this:
sessionFactory = MyFactory.getSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
School mbs = new School("interesting school");
Address a = new Address("garden street 5","12345", "somewhere");
mbs.setAddress(a);
session.save(mbs);
tx.commit();
This worked before with XML, but now doesn't..
The first Hibernate query action visible on the console is:
Hibernate: insert into schools (address_id, layout_id, name) values (?, ?, ?)
Therefore an error occurs: ERROR: Column 'address_id' cannot be null
How can I fix this?
.
Those are the tables:
CREATE TABLE schools(
id INTEGER NOT NULL AUTO_INCREMENT,
name CHAR(50),
address_id INTEGER NOT NULL,
layout_id INTEGER NOT NULL,
CONSTRAINT fk_address FOREIGN KEY (address_id) REFERENCES addresses(id),
CONSTRAINT fk_layout FOREIGN KEY (layout_id) REFERENCES layout_headers(id),
PRIMARY KEY (id)
);
CREATE TABLE addresses(
id INTEGER NOT NULL AUTO_INCREMENT,
street CHAR(55),
zip CHAR(6),
city CHAR(60),
CONSTRAINT addr_pk PRIMARY KEY (id)
);
In my hibernte.cfg.xml I have the following:
<hibernate-configuration>
<session-factory>
...
<mapping class="creator.models.school.Address" />
<mapping class="creator.models.school.Report" />
<mapping class="creator.models.school.School" />
<mapping class="creator.models.school.SchoolClass" />
...
</session-factory>
</hibernate-configuration>
You should specify cascade property of #ManyToOne annotation on address field of School class to save address before school. For example, CascadeType.ALL

Categories

Resources