I am learning JPA with hibernate these days. I am not able to understand why hibernate gives error for a Bidirectional OnetoMany relationship if mappedBy attribute is not specified. Following is the code on which I am getting error:
#Entity
public class Item {
#Id
private long id;
private String name;
private String description;
#OneToMany()
Set<Bid> bids = new HashSet<Bid>();
Bid is the child entity of ITEM
#Entity(name="BIDS")
public class Bid {
#Id
#Column(name="BID_ID")
private Long id;
private Double amount;
#ManyToOne
#JoinColumn(name="ITEM_ID")
Item item;
In the main class I am saving both parent and child entities:
Transaction transaction = session.beginTransaction();
Item item = new Item();
item.setId(111);
item.setDescription("ITEM Description");
item.setName("Name1");
Bid bid = new Bid();
bid.setId(21l);
bid.setAmount(1.1);
bid.setItem(item);
Set<Bid> bids = new HashSet<Bid>();
bids.add(bid);
item.setBids(bids);
session.save(item);
session.save(bid);
transaction.commit();
But hibernate tries to execute following queries and throws excetion that ITEM_BIDS table do not exist.
Hibernate: insert into Item (description, name, id) values (?, ?, ?)
Hibernate: insert into BIDS (amount, ITEM_ID, BID_ID) values (?, ?, ?)
Hibernate: insert into Item_BIDS (Item_id, bids_BID_ID) values (?, ?)
Please tell me why hibernate is generating extra query and how mappedBy element will solve this.
Because if you don't specify mappedBy, you're not saying that the OneToMany between Item and Bid and the ManyToOne between Bid and Item are actually the two sides of a unique bidirectional association.
So Hibernate considers that they are two, different, unidirectional associations. And since the default mapping for a OneToMany association is to use a join table, that's what Hibernate uses.
Related
I am quite new to hibernate. I have created two entities like user and vehicle with user having one to many relationship with vehicle.
#OneToMany
#JoinColumn(name="Vehicle_id")
Collection<Vehicle> vehicle = new ArrayList<>();
and adding them to table like this
UserInfo user = new UserInfo();
user.setUsername(username);
user.setPassword(password);
user.setDob(dob);
Vehicle vehicle = new Vehicle();
vehicle.setVehicleName("AUdi");
user.getVehicle().add(vehicle);
Vehicle vehicle2 = new Vehicle();
vehicle2.setVehicleName("BMW");
user.getVehicle().add(vehicle2);
SessionFactory sessionfactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
session.save(user);
session.save(vehicle);
session.save(vehicle2);
session.getTransaction().commit();
session.close();
But I am getting result like
Hibernate: insert into UserInformation (user_name, DOB) values (?, ?)
Hibernate: insert into Vehicle (vehicleName) values (?)
Hibernate: insert into Vehicle (vehicleName) values (?)
Hibernate: update Vehicle set Vehicle_id=? where vehicleID=?
Hibernate: update Vehicle set Vehicle_id=? where vehicleID=?
There is no table created like
insert into User_vehicle(User_id,vehicle_id) Values (?,?)
So I am not getting any table name User_vehicle in db.
Hope you understand my question.
You have specified #JoinColumn for a #OneToMany association, thus there will be a foreign key column on the many side. That is the recommended approach actually.
If you need to use join table for #OneToMany association, then you need to omit #JoinColumn and optionally specify #JoinTable to override the default names for the table and columns:
#OneToMany
#JoinTable(
name="User_Vehicle",
joinColumns = #JoinColumn(name = "User_Id"),
inverseJoinColumns = #JoinColumn(name = "Vehicle_Id")
)
Collection<Vehicle> vehicles = new ArrayList<>();
I'm using Hibernate 4.3.10.Final (with SpringData JPA) running on a Postgres 4 database and have run into a very strange bug. Our app utilizes a database outside of the default "public" schema, and when we try to insert data Hibernate drops the correct schema.
Our model consists of an abstract "Log" class that uses single class inheritance to allow many different object types to insert a associated log message. See code below.
The schema already exists (hibernate doesn't create it) and booting validation runs fine, but when try to insert a new record we get the error relation "booking_log" does not exist -- which is missing the schema modifier (say customapp for our purposes). See the first line from the logs below to get an idea of what other insert statements look like.
I've dug through the mapping phase and verified Hibernate is indeed picking up the schema from the #JoinTable annotation, but not sure how we're losing it.
Any help debugging or possible solutions would be appreciated.
Thanks!
Log - Abstract super class
#MappedSuperclass
#Table(name="log", schema=Constants.DB_SCHEMA)
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name="log_type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Log {
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="log_seq_gen")
#SequenceGenerator(allocationSize = 1, name="log_seq_gen", sequenceName=Constants.DB_SCHEMA + ".log_id_seq")
private Long id;
// ...
}
BookingLog
#Entity
#DiscriminatorValue("2")
public class BookingLog extends Log implements TenantResource<Company,Long> {
#JoinTable(name="booking_log",
schema = Constants.DB_SCHEMA,
joinColumns = { #JoinColumn(name="log_id", referencedColumnName="id", nullable=false, updatable=false)},
inverseJoinColumns = { #JoinColumn(name="booking_id", referencedColumnName="id", nullable=false, updatable=false)})
#ManyToOne(cascade=CascadeType.ALL)
private Booking booking;
///...
}
** Logs **
2015-07-20_18:14:09.055 DEBUG org.hibernate.SQL - insert into customapp.booking_product (created_dt, created_by, modified_dt, modified_by, include_in_payroll, include_in_revenue, booking_id, description, payroll_percent, price, product_id, qty, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2015-07-20_18:14:09.072 DEBUG org.hibernate.SQL - insert into booking_log (log_date, details, log_time, user_id, booking_id, id) values (?, ?, ?, ?, ?, ?)
2015-07-20_18:14:09.176 DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
org.postgresql.util.PSQLException: ERROR: relation "booking_log" does not exist
Based on your #JoinTable configuration and the insert statement that hibernate generates looks like the problem is the way you are triyng to add extra fields/data to the booking_log table.
I would need more details about your model to be sure but I think you are using a join-table and something else instead of create a class that models the join-table.
I mean, you have this
BookingLog (*) --------------------------------------> (1) Booking
but I think you really need this
BookingLog (1) ---> (1) BookingLogAssociation (*) ---> (1) Booking
Then the mapping will be like this,
#Entity
#DiscriminatorValue("2")
public class BookingLog extends Log implements TenantResource<Company,Long> {
#OneToOne(mappedBy="bookingLog")
private BookingLogAssociation booking;
}
Note the attributes of BookingLogAssociation, they are the extra field/data you want to add in booking_log table.
#Entity
#Table(name="booking_log")
#IdClass(BookingLogAssociationId.class)
public class BookingLogAssociation {
#Id
private long log_id;
#Id
private long booking_id;
#OneToOne
#PrimaryKeyJoinColumn(name="log_id", referencedColumnName="id")
private BookingLog bookingLog;
#ManyToOne
#PrimaryKeyJoinColumn(name="booking_id", referencedColumnName="id")
private Booking booking;
#Column(name="log_date")
#Temporal(TemporalType.DATE)
private Calendar logDate;
#Column(name="log_time")
#Temporal(TemporalType.TIME)
private Calendar logTime;
#ManyToOne
#JoinColumn(name="user_id")
private User user;
// Could be just an attribute too
//#Column(name="user_id")
//private long userId;
...
}
The BookingLogAssociationId class that represents the BookingLogAssociation's composite key.
public class BookingLogAssociationId implements Serializable {
private long log_id;
private long booking_id;
public int hashCode() {
return (int)(log_id + booking_id);
}
public boolean equals(Object object) {
if (object instanceof BookingLogAssociationId) {
BookingLogAssociationId otherId = (BookingLogAssociationId) object;
return (otherId.log_id == this.log_id) && (otherId.booking_id == this.booking_id);
}
return false;
}
You can read more about this option here
I am trying to create a simple example to understand how collection of basic and embaddable types works in Hibernate.
I have created a User entity with a set of nickNames and also a set of addresses. Here are my Java classes:
User.java
#Entity
#Table(name = "TB_User")
public class User {
#Id
#GeneratedValue
private int id;
private String name;
#ElementCollection
#CollectionTable(name = "Nicknames", joinColumns = #JoinColumn(name = "user_id"))
#Column(name = "nickname")
private Set<String> nickNames = new HashSet<String>();
#ElementCollection
#CollectionTable(name = "Addresses", joinColumns = #JoinColumn(name = "user_id"))
#AttributeOverrides({ #AttributeOverride(name = "street1", column = #Column(name = "fld_street")) })
public Set<Address> addresses = new HashSet<Address>();
public User() {
}
public User(String name, Address... addresses) {
this.name = name;
this.addresses.addAll(Arrays.asList(addresses));
}
public void addNickName(String... nickNames) {
this.nickNames.addAll(Arrays.asList(nickNames));
}
// Setters & Getters
}
Address.java
#Embeddable
public class Address {
private String street1;
public Address() {}
public Address(String street1) {
this.street1 = street1;
}
#Override
public String toString() {
return street1;
}
// Setters & Getters
}
Now I have created a simple program to create users in database and then show the list of users. Here is my code:
private static void saveUsers() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
User user = new User("User", new Address("abc"),
new Address("xyz"));
user1.addNickName("alpha", "beta");
session.save(user);
session.getTransaction().commit();
}
private static void showUsers() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
List<User> users = session.createQuery("from User").list();
for (User user : users) {
System.out.println(user.getName() + " -- > " + user.getNickNames()
+ " --> " + user.getAddresses());
}
session.getTransaction().commit();
}
When I run this program, I observed that hibernate issues below set of commands:
Hibernate: select user0_.id as id1_2_, user0_.name as name2_2_ from TB_User user0_
Hibernate: select nicknames0_.user_id as user_id1_2_0_, nicknames0_.nickname as nickname2_1_0_ from Nicknames nicknames0_ where nicknames0_.user_id=?
Hibernate: select addresses0_.user_id as user_id1_2_0_, addresses0_.fld_street as fld_street2_0_0_ from Addresses addresses0_ where addresses0_.user_id=?
User -- > [alpha, beta] --> [xyz, abc]
Hibernate: delete from Addresses where user_id=?
Hibernate: insert into Addresses (user_id, fld_street) values (?, ?)
Hibernate: delete from Addresses where user_id=?
Hibernate: insert into Addresses (user_id, fld_street) values (?, ?)
If I try to get the list of addresses using user.getAddresses() with in session, then Hibernate deletes & re-inserts records in Addresses table.
Why hibernate tries to delete and re-create records in Addresses table, as this causes performance issue. Also why it is not applicable to basic types like nickNames in my example and not running update commands for the property nickNames?
This behaviour is related to the fact, that there is not bi-directional mapping. To understand that in depth - please read this article:
inverse = “true” example and explanation
And here is the way how to do that with annotation:
inverse=true in JPA annotations
Let me cite from the answer:
I found an answer to this. The mappedBy attribute of #OneToMany annotation behaves the same as inverse = true in the xml file.
Some summary:
Hibernate can issue SQL statements which we would expect. But only, if we do have bidirectional mapping in place.
Then, the other end (address) will be driving the persistence. It will/must know about its parent - and that's why some direct UPDATE statements could be issued.
So, if we want to avoid DELETE and INSERT, we have to use the inverse mapping. Hibernate will issue more "expectable" SQL.
This one is quite old, but might be relevant to sombody. Another tip that helped in my case: if you can separate reads from writes, consider using
#Transactional(readOnly = true)
That makes underlying JPA framework know that no modifications are expected in the current transaction. In practice, Hibernate no longer would run delete + insert when you merely want fetching some data.
Here is my code. I would like to generate an automatic ID based on parent class. I'm using a method to create Airport, so my ID it's coming with is a null value. ID in AirportModel will be generated, but I don't know how to make it in child class.
#Entity(name = "Airport")
#Table(name = "ai_airport")
public class AirportModel {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", nullable = false)
private Long id;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "airport_id")
private List<AirportTranslatedModel> translations;
Second class(child):
#Entity(name = "AirportTranslated")
#IdClass(AirportTranslatedModelKey.class)
#Table(name = "ai_translated_airport")
public class AirportTranslatedModel
#Id
#Column(name="airport_id")
private Long airportId;
#Id
#Column(name="language_code", length=2)
private String languageCode;
Third one(keys):
#Embeddable
public class AirportTranslatedModelKey implements Serializable {
#Column(name="airport_id")
private Long airportId;
#Column(name="language_code", length=2)
private String languageCode;
I still got the same errors; log:
Hibernate: insert into ai_airport (active, airport_code, city_code, country_code, externa
l_id, is_default, latitude, longitude, market_code, min_connection_time_DD, min_connection_time_DI, min_connection_time_id, min_connection_time_II, time_diff, VERSION) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ai_translated_airport (airport_long_name, airport_short_name, airp
ort_id, language_code) values (?, ?, ?, ?)
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'airport_id' cannot be null
Your current setup has the AirportTranslatedModel airport_id field mapped through a Long- you will need to set the airportId manually to have it set the id in the database. This will likely require that you persist AirportModel and possibly flush to have its PK assigned and available prior to making the AirportModel->AirportTranslatedModel association, so that you can then set the AirportTranslatedModel.airportId.
JPA 2 though allows derived Ids. If you want AirportTranslatedModel to have its ID assigned from AirportModel, it needs to have a relationship to it. There is a simple example at http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers
If you were to model your classes in a similar fashion, it might look like:
public class AirportModel {
..
#OneToMany(mappedby="airportModel", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AirportTranslatedModel> translations;
..
}
public class AirportTranslatedModel {
#Id
#JoinColumn(name="airport_id")
private AirportModel airportModel;
#Id
#Column(name="language_code", length=2)
private String languageCode;
..
}
public class AirportTranslatedModelKey implements Serializable {
private Long airportModel;
private String languageCode;
}
Notice that there is no need to make the AirportTranslatedModelKey and embeddable if you are just using it as a pk class. Also note that the AirportTranslatedModelKey contains a Long airportModel - this must match the type of the pk in the AirportModel, and the name of the relationship property in AirportTranslatedModel.
This will allow AirportTranslatedModel to pull the airport_id value from AirportModel and use it as its PK even though it might not have been generated yet when both entities are still new.
can anyone tell me where is the error in this example
#Entity
#Table(name = "ITEM")
public class Item implements Serializable
{
#Id
#GeneratedValue
#Column(name = "ID")
private Long id;
#Column(name = "NAME")
private String name;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "ID_ITEM",referencedColumnName="ID")
private List<ItemDetail> itemDetails;
second class
#Entity
#Table(name = "ITEM_DETAIL")
public class ItemDetail implements Serializable
{
#Id
#GeneratedValue
#Column(name = "ID")
private Long id;
#Column(name = "NAME")
private String name;
#Column(name = "ID_ITEM")
private Long itemId;
and the db
COMMIT;
CREATE TABLE item(
id serial PRIMARY KEY,
name VARCHAR(16)
);
CREATE TABLE item_detail(
ID serial PRIMARY KEY,
NAME VARCHAR(16),
ID_ITEM serial REFERENCES item (id)
);
COMMIT;
The error i got is
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into ITEM (NAME, ID) values (?, ?)
Hibernate: insert into ITEM_DETAIL (ITEM_ID, NAME, ID) values (?, ?, ?)
Hibernate: insert into ITEM_DETAIL (ITEM_ID, NAME, ID) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.mkyong.common.App.main(App.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into ITEM_DETAIL (ITEM_ID, NAME, ID) values (NULL, id1, 161) was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2530)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1317)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2592)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 13 more
Process finished with exit code 1
It's obviously that item_id is null but why??
Thanks
Regards
#backebg: can you check your db script and let us know that you executed exactly same in Database. If yes then either correct your entities to use "ID_ITEM" or item_detail table to use 'ITEM_ID' instead 'ID_ITEM'. thanks
Having ITEM_ID mapped as a column in ItemDetail is a bit odd, and mapping it that way might might be the source of the problem. Nothing is telling the ItemDetail class that that field should be populated with a proper id for the parent Item, including that it shouldn't be null.
If the detail doesn't need to know about the parent, you might be able to just omit that field in the ItemDetail java code altogether. The field in the table should be populated as a consequence of the relation.
It's more common to map this sort of thing as a bidirectional association, so that you have a #OneToMany relation of Item to ItemDetail and a #ManyToOne relation of ItemDetail to Item, and the relations can be navigated in Java. If the ItemDetail does need to know about the parent item, you should do it this way.
This is described somewhere in the Hibernate Annotations Reference section on mapping associations.
Use nullable = false to tell Hibernate that the join column cannot be null:
#Entity
#Table(name = "ITEM")
public class Item implements Serializable {
// ...
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "ID_ITEM", referencedColumnName = "ID", nullable = false)
private List<ItemDetail> itemDetails;
// ...
}
and remove the itemId property from ItemDetail as it is already mapped by the #JoinColumn annotation. If you need the itemId, then use a bi-directional relationship (hold a reference to the entire Item object, not just the ID).