MappedBy in a OneToOne Mapping - java

I have created Three classes- User, UserSubscription and Subscription. I want that users in the UserSubscription table should be referenced by the users existing in the User table. So, how to use mappedBy?
The classes are as follows:
User:
package com.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
#Entity
public class User {
private int userId;
private String password;
private String contact;
private String firstName;
private String lastName;
private String gender;
private String address;
private int age;
private String email;
private String countBooks;
private Set<Language> languages = new HashSet<Language>(0);
private Set<Book> favorites = new HashSet<Book>(0);
#Id
#GeneratedValue
#Column(name="User_Id")
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
#Column(name="Password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Column(name="Contact")
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
#Column(name="First_Name")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name="Last_Name")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name="Gender")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
#Column(name="Address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
#Column(name="Age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Column(name="Email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Column(name="Count_Books")
public String getCountBooks() {
return countBooks;
}
public void setCountBooks(String countBooks) {
this.countBooks = countBooks;
}
#ManyToMany(cascade=CascadeType.ALL)
#JoinTable(name="User_Language", joinColumns = {
#JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "Language_Id",
nullable = false, updatable = false) })
public Set<Language> getLanguages() {
return languages;
}
public void setLanguages(Set<Language> languages) {
this.languages = languages;
}
#ManyToMany
#JoinTable(name="User_Favorite", joinColumns = {
#JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "Book_Id",
nullable = false, updatable = false) })
public Set<Book> getFavorites() {
return favorites;
}
public void setFavorites(Set<Book> favorites) {
this.favorites = favorites;
}
}
UserSubscription:
package com.model;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
#Entity
public class UserSubscription {
private int userSubscriptionId;
private Subscription subscription;
private User user;
private Date subscriptionStartDate;
private Date subscriptionEndDate;
private int status;
#Id
#GeneratedValue
#Column(name="User_Subscription_Id")
public int getUserSubscriptionId() {
return userSubscriptionId;
}
public void setUserSubscriptionId(int userSubscriptionId) {
this.userSubscriptionId = userSubscriptionId;
}
#Column(name="Subscription_Id")
public Subscription getSubscription() {
return subscription;
}
public void setSubscription(Subscription subscription) {
this.subscription = subscription;
}
#OneToOne(mappedBy="userId")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
#Column(name="Subscription_Start_Date")
public Date getSubscriptionStartDate() {
return subscriptionStartDate;
}
public void setSubscriptionStartDate(Date subscriptionStartDate) {
this.subscriptionStartDate = subscriptionStartDate;
}
#Column(name="Subscription_End_Date")
public Date getSubscriptionEndDate() {
return subscriptionEndDate;
}
public void setSubscriptionEndDate(Date subscriptionEndDate) {
this.subscriptionEndDate = subscriptionEndDate;
}
#Column(name="Status")
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Subscription:
package com.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
public class Subscription {
private int subscriptionId;
private String name;
private int period;
private float fees;
private int noOfBooks;
private int Status;
#Id
#GeneratedValue
public int getSubscriptionId() {
return subscriptionId;
}
public void setSubscriptionId(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public float getFees() {
return fees;
}
public void setFees(float fees) {
this.fees = fees;
}
public int getNoOfBooks() {
return noOfBooks;
}
public void setNoOfBooks(int noOfBooks) {
this.noOfBooks = noOfBooks;
}
public int getStatus() {
return Status;
}
public void setStatus(int status) {
Status = status;
}
}
Exception while executing:
Exception in thread "main" org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.model.User.userId in mappedBy of com.model.UserSubscription.user
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:248)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at com.impetus.model.Check.main(Check.java:92)

In your code, user-user_subscription looks uni-directional. 'mappedby' should be used in bi-directional relationship.
You can use following code in user entity to make it bi-directional with user_subscription.
private UserSubscription userSubscription;
#OneToOne
public User getUserSubscription() {
return userSubscription;
}
Or remove mappedby from user_subscription and make it unidirectional.

Seems you are unsure of what mappedBy is used for or means. Database relationships are one sided; one table will have a foreign key reference to the target table. In JPA, you might map this relationship as a OneToOne and specify the foreign key to use with the #JoinColumn annotation:
#OneToOne(mappedBy="userId")
#JoinColumn(name = "USER_ID")
public User getUser() {
return user;
}
The Join column definition is optional, and if not used JPA will default to using the attribute name with "_" and the target entity's ID name prepended, so "USER_ID" in this case anyway.
Back to the database, because target table is referenced by the source table, this forms another implicit relationship back (the target table's ID can be used to look up the other table's foreign key). In JPA, this can be mapped again as a OneToOne or a OneToMany depending on the cardinality. If it is a OneToOne, you specify that this relationship does not define the foreign key by marking it as mappedBy the other relationship:
#OneToOne(mappedBy="user")
public UserSubscription getUserSubscription() {
return userSubscription;
}
But it is more likely a User will have multiple userSubscriptions:
#OneToMany(mappedBy="user")
public Collection<UserSubscription> getUserSubscriptions () {
return userSubscription ;
}
In both cases, you use the mappedBy to signal that there is a 'user' attribute in the target entity that defines a relationship to this entity and controls the foreign key setting in the database.
You also need to change how your define the UserSubscription->Subscription relationship, as you have defined an #Column on what appears to be a relationship. It should probably be
#OneToOne
#JoinColumn(name="Subscription_Id")
public Subscription getSubscription() {
return subscription;
}

you should have UserSubscription memeber and corrosponding gettes and setters in your User class like below:
private Set<Company> userSubscriptions = new HashSet<Company>(0);
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserSubscription> getUserSubscriptions() {
return this.userSubscriptions ;
}
public void setUserSubscriptions(Set<UserSubscription> userSubscriptions ) {
this.userSubscriptions = userSubscriptions ;
}
and same way you should have user object as memeber in UserSubscription Entity class.
private User user;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "USER_ID", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}

You are doing it wrong
class User {
UserSubscription userSubscription ;
#OneToOne
public UserSubscription getUserSubscription(){
return userSubscription ;
}
public void setUserSubscription(UserSubscription userSubscription ){
this.userSubscription = userSubscription ;
}
}
class UserSubscription {
#OneToOne(mappedBy="userSubscription")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

Related

Cannot delete or update a parent row: a foreign key constraint fails when deleting entity with reference

I want to delete an instance of "UserRegistration" which has a reference to an instance of "Authority". When deleting the instance of "UserRegistration" via the repository I get following Error:
"Cannot delete or update a parent row: a foreign key constraint fails (seprojekt.authorities, CONSTRAINT authorities_ibfk_1 FOREIGN KEY (userid) REFERENCES users (id))"
Is there a possibility of deleting the referenced Entity when the Instance of "UserRegistration" ist deleted? I tried this with Cascadetype and orphanRemoval in "UserRegistration" but nothing happened.
The Method which tries to delete the Entity:
#DeleteMapping
public boolean deleteUser(#RequestParam String username) {
Optional<UserRegistration> optUserRegistration = uRepo.findByUsername(username);
if(optUserRegistration.isPresent()) {
System.out.println("!!! Authority is: '" + optUserRegistration.get().getAuthority().getAuthority());
uRepo.delete(optUserRegistration.get());
return true;
}
return false;
}
The UserRegistrationclass looks like this:
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
#Entity
#Table(name = "users")
public class UserRegistration {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
#JsonIgnore
private String password;
#JsonIgnore
private boolean enabled;
private Integer profilePicture;
private String email;
#OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "id")
private Authority authority;
#JsonManagedReference
#ManyToMany
#JoinTable(name = "userhasfavorite", joinColumns = #JoinColumn(name = "userid"), inverseJoinColumns = #JoinColumn(name = "channelid"))
private List<Channel> favorites;
public UserRegistration() {
}
public UserRegistration(String email, String username, String password, boolean enabled, int profilePicture) {
this.email = email;
this.username = username;
this.password = password;
this.enabled = enabled;
this.profilePicture = profilePicture;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Integer getProfilePicture() {
return profilePicture;
}
public void setProfilePicture(Integer profilePicture) {
this.profilePicture = profilePicture;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
public List<Channel> getFavorites() {
return favorites;
}
public void setFavorites(List<Channel> favorites) {
this.favorites = favorites;
}
}
The Authorityclass looks like this:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
#Entity
#Table(name = "authorities")
public class Authority {
#Id
#Column(name = "userid")
private int userID;
private String username;
private String authority;
#OneToOne(mappedBy = "authority")
private UserRegistration user;
public Authority() {
}
public Authority(int userID, String username, String authority) {
this.userID = userID;
this.username = username;
this.authority = authority;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public UserRegistration getUser() {
return user;
}
public void setUser(UserRegistration user) {
this.user = user;
}
}
Thanks a lot for reading this. Please ask me for more details if required.
In order for cascading to work; you need to reverse the entity relation definition. The cascading only works for parent entity to child entity. What I mean is the following. Please define authority in UserRegistration as:
#OneToOne(mappedBy="user", cascade = CascadeType.ALL)
private Authority authority;
and the userRegistration in Authority as:
#OneToOne
#MapsId
private UserRegistration user;
I believe this should work. Take a look at the following for more information. Hope that helps.

Table Column Mapping error on Hibernate with spring boot

I have two table on MySql database. Item, ItemGroup which are related by Item.groupid -> ItemGroup.id I am also using hibernate-entitymanager and spring boot. Here are my entity classes for Item and ItemGroup.
Item.java
package com.thiha.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
#Entity
public class Item {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String createdDate;
private String modifiedDate;
private String itemCode;
private String description;
private int groupId;
private double quantity;
private double price;
private int recordStatus;
private ItemGroup itemGroup;
#ManyToOne
#JoinColumn(name = "groupid")
public ItemGroup getItemGroup() {
return itemGroup;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(String modifiedDate) {
this.modifiedDate = modifiedDate;
}
public String getItemCode() {
return itemCode;
}
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getRecordStatus() {
return recordStatus;
}
public void setRecordStatus(int recordStatus) {
this.recordStatus = recordStatus;
}
}
ItemGroup.java
package com.thiha.entities;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
#Entity
public class ItemGroup {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String createdDate;
private String modifiedDate;
private String groupCode;
private String groupName;
private int recordStatus;
private List<Item> items;
#OneToMany(mappedBy = "itemGroup", cascade = CascadeType.ALL)
public List<Item> getItems(){
return items;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(String modifiedDate) {
this.modifiedDate = modifiedDate;
}
public String getGroupCode() {
return groupCode;
}
public void setGroupCode(String groupCode) {
this.groupCode = groupCode;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public int getRecordStatus() {
return recordStatus;
}
public void setRecordStatus(int recordStatus) {
this.recordStatus = recordStatus;
}
}
I also create a foreign keys in Item table reference to ItemGroup table with foreign key groupid. But when I run the code with mvn spring-boot:run, I got some exception as following:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: item_group, for columns: [org.hibernate.mapping.Column(items)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)
at org.hibernate.mapping.Property.isValid(Property.java:225)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
If I remove the relationships in both class, it is working properly but I cannot make INNER JOIN query. Please help me to solve this problem
You can't mix annotation conventions in Hibernate. Please try to move your #Id and #GeneratedValue annotations to getter, or #OneToMany and #ManyToOne to fields.
It says
Could not determine type for: java.util.List, at table: item_group, for columns: [org.hibernate.mapping.Column(items)]'
Because your mappedBy is incorrect.
When you put annotation on getter getCategory(), hibernate understands that it's category.
#ManyToOne
#JoinColumn(name = "groupid")
public ItemGroup getCategory() {
return itemGroup;
}
So, please use appropriate name in your class ItemGroup
#OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
public List<Item> getItems(){
return items;
}
dont worry there is do only one thing remove your annotation of #Id and #generatorValue annotation above on the id and put on the getter method of your id
the problem will be fix.....!

Data not persisting in join column using spring-data

The hibernate output is as follows.
Hibernate: insert into usernew (ACTIVE, EMAIL, FIRST_NAME, CGUID, LAST_NAME, MIDDLE_NAME, PIN) values (?, ?, ?, ?, ?, ?, ?)
Hibernate:insert into usernew (ACTIVE, EMAIL, FIRST_NAME, CGUID, LAST_NAME,MIDDLE_NAME, PIN) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into eventnew (ACTIVE, ADDRESS, DESCRIPTION, END_TS, HOST_PHONE_NUMBER,IMAGE, LATITUDE, LONGITUDE, NAME, PLACE_ID, START_TS) values (?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?)
User user = new User();
user.setActive(true);
user.setFirstName("test");
user.setLastName("test");
user.setPin("3333");
user.setEmail("n#f.com");
userRepositoryNew.save(user);
User user1 = new User();
user1.setActive(true);
user1.setFirstName("test");
user1.setLastName("test");
user1.setPin("3333");
user1.setEmail("nauman#f1.com");
userRepositoryNew.save(user1);
Event event = new Event();
event.setName("event");
event.setDescription("testing event");
event.setHostPhoneNumber("4455-33-990");
event.setLongitude("dddd");
EventUser eventUser = new EventUser();
eventUser.setUser(user);
eventUser.setEvent(event);
EventUser eventUser1 = new EventUser();
eventUser.setUser(user1);
eventUser.setEvent(event);
event.getEventUsers().add(eventUser);
event.getEventUsers().add(eventUser1);
eventRepositoryNew.save(event);
Below is the code, not sure why join table is not getting populated.
package com.hive.domain;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.hive.json.marshaller.DateUTCDeserializer;
import com.hive.json.marshaller.DateUTCSerializer;
import com.hive.json.marshaller.ImageDeserializer;
import com.hive.json.marshaller.ImageSerializer;
#Entity
#Table(name = "eventnew")
public class Event implements java.io.Serializable {
private Integer id;
private String name;
private String description;
private String address;
private String latitude;
private String longitude;
#JsonSerialize(using = ImageSerializer.class)
#JsonDeserialize(using = ImageDeserializer.class)
private byte[] image;
private String placeId;
#JsonSerialize(using = DateUTCSerializer.class)
#JsonDeserialize(using = DateUTCDeserializer.class)
private Date startAt;
#JsonSerialize(using = DateUTCSerializer.class)
#JsonDeserialize(using = DateUTCDeserializer.class)
private Date endAt;
private boolean active;
private String hostPhoneNumber;
private Set<User> participants = new HashSet<User>(0);
private Set<EventUser> eventUsers = new HashSet<EventUser>(0);
public Event() {
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name = "NAME", nullable = false, length = 10)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Column(name = "DESCRIPTION")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Column(name = "ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
#Column(name = "LATITUDE")
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
#Column(name = "LONGITUDE")
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
#Column(name = "START_TS")
public Date getStartAt() {
return startAt;
}
public void setStartAt(Date startAt) {
this.startAt = startAt;
}
#Column(name = "END_TS")
public Date getEndAt() {
return endAt;
}
public void setEndAt(Date endAt) {
this.endAt = endAt;
}
#Column(name = "IMAGE")
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
#Column(name = "PLACE_ID")
public String getPlaceId() {
return placeId;
}
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
#Column(name = "ACTIVE")
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
#Column(name = "HOST_PHONE_NUMBER")
public String getHostPhoneNumber() {
return hostPhoneNumber;
}
public void setHostPhoneNumber(String hostPhoneNumber) {
this.hostPhoneNumber = hostPhoneNumber;
}
#Transient
public Set<User> getParticipants() {
return participants;
}
public void setParticipants(Set<User> participants) {
this.participants = participants;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.event")
public Set<EventUser> getEventUsers() {
return this.eventUsers;
}
public void setEventUsers(Set<EventUser> eventUsers) {
this.eventUsers = eventUsers;
}
}
package com.hive.domain;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
#Entity
#Table(name = "usernew")
public class User implements java.io.Serializable {
private String guid;
private String firstName;
private String middleName;
private String lastName;
private Integer phoneNumber;
private String email;
private String pin;
private boolean active;
private Set<EventUser> eventUsers = new HashSet<EventUser>(0);
public User() {
}
#Column(name = "CGUID")
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "PHONE_NUMBER", unique = true, nullable = false)
public Integer getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(Integer phoneNumber) {
this.phoneNumber = phoneNumber;
}
#Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name = "MIDDLE_NAME")
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
#Column(name = "EMAIL")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Column(name = "PIN")
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
#Column(name = "ACTIVE")
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade = CascadeType.ALL)
public Set<EventUser> getEventUsers() {
return this.eventUsers;
}
public void setEventUsers(Set<EventUser> eventUsers) {
this.eventUsers = eventUsers;
}
}
package com.hive.domain;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
#Entity
#Table(name = "eventusernew", catalog = "hive")
#AssociationOverrides({ #AssociationOverride(name = "pk.user", joinColumns = #JoinColumn(name = "USER_ID") ),
#AssociationOverride(name = "pk.event", joinColumns = #JoinColumn(name = "EVENT_ID") ) })
public class EventUser implements java.io.Serializable {
private EventUserId pk = new EventUserId();
// private String state;
// private String comment;
// private String displayName;
public EventUser() {
}
// #Column(name = "STATE")
// public String getState() {
// return state;
// }
//
// #Column(name = "DISPLAY_NAME")
// public String getDisplayName() {
// return displayName;
// }
//
// public void setDisplayName(String displayName) {
// this.displayName = displayName;
// }
//
// public void setState(String state) {
// this.state = state;
// }
//
// #Column(name = "COMMENT")
// public String getComment() {
// return comment;
// }
//
// public void setComment(String comment) {
// this.comment = comment;
// }
#EmbeddedId
public EventUserId getPk() {
return pk;
}
public void setPk(EventUserId pk) {
this.pk = pk;
}
#Transient
public User getUser() {
return getPk().getUser();
}
public void setUser(User stock) {
getPk().setUser(stock);
}
#Transient
public Event getEvent() {
return getPk().getEvent();
}
public void setEvent(Event category) {
getPk().setEvent(category);
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
EventUser that = (EventUser) o;
if (getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null)
return false;
return true;
}
public int hashCode() {
return (getPk() != null ? getPk().hashCode() : 0);
}
}
package com.hive.domain;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
#Embeddable
public class EventUserId implements java.io.Serializable {
private User user;
private Event event;
#ManyToOne
public User getUser() {
return user;
}
public void setUser(User stock) {
this.user = stock;
}
#ManyToOne
public Event getEvent() {
return event;
}
public void setEvent(Event category) {
this.event = category;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EventUserId that = (EventUserId) o;
if (user != null ? !user.equals(that.user) : that.user != null) return false;
if (event != null ? !event.equals(that.event) : that.event != null)
return false;
return true;
}
public int hashCode() {
int result;
result = (user != null ? user.hashCode() : 0);
result = 31 * result + (event != null ? event.hashCode() : 0);
return result;
}
}
you need to cascade your save.
In your Event entity, change the one-to-many to something like this :
#OneToMany(mappedBy="pk.event", cascade=CascadeType.ALL)

Join in hibernate with annotaion

I am trying to perform join in hibernate and i am using struts2.
I am working with hibernate using annotaions. Now i am unable to perform join between two tables.My first table is "studentprojects" which contain pid and email.Second table is "initialprojectdetials" which contains pid,name,description... similarly some other fields.I have to get the data of second table by performing join around pid of first table.
For this am using this query:
String hql="from InitialProjectDTO I join I.projectId S where I.projectId=:id";
Query query=session.createQuery(hql);
query.setParameter("id", id);
mail =query.list();
where mail is the arraylist of InitialProjectDTO.
And my InitialProjectDTO is:
package edu.pma.dto;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name="initialprojectdetail")
public class InitialProjectDTO {
#Id
#Column(name="projectId")
#OneToMany(cascade=CascadeType.ALL)
#JoinTable(name="studentprojects",joinColumns=#JoinColumn(name="projectId"))
int projectId;
#Column(name="name")
String name;
#Column(name="description")
String description;
#Column(name="technology")
String technology;
#Column(name="guide")
String guide;
#Column(name="duration")
int duration;
#Column(name="status")
String status;
#Column(name="report")
String report;
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTechnology() {
return technology;
}
public void setTechnology(String technology) {
this.technology = technology;
}
public String getGuide() {
return guide;
}
public void setGuide(String guide) {
this.guide = guide;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
my SudentProjectDTO is:
package edu.pma.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="studentprojects")
public class StudentProjectDTO {
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
#Id
#Column(name="email")
String email;
#Column(name="projectId")
int projectId;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
This is the error which i am getting:
Illegal attempt to map a non collection as a #OneToMany, #ManyToMany or #CollectionOfElements: edu.pma.dto.InitialProjectDTO.projectId
Method "execute" failed for object edu.pma.actions.LoginAction#1096a56
File: org/hibernate/cfg/annotations/CollectionBinder.java
You should try to use different models
#Entity
public class InitialProjectDTO {
#OneToMany(mappedBy = "project")
private Collection<StudentProjectDTO> students;
}
#Entity
public class StudentProjectDTO {
#ManyToOne
private InitialProjectDTO project;
}
And with the proper model it shuld be easy to write hql, you might want to look here for examples https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html.
Also I would suggest to look here for example of models. http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/
See following example might its help to you
#Entity
#Table(name="initialprojectdetail")
public class InitialProjectDTO {
private Integer initialProjectDTOId;
private Set<StudentProjectDTO > studentProjectDTO = new HashSet<StudentProjectDTO >(0);
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "initial_projectDTO_id", unique = true, nullable = false)
public Integer getInitialProjectDTOId() {
return this.initialProjectDTOId;
}
public void setInitialProjectDTOId(Integer initialProjectDTOId) {
this.initialProjectDTOId = initialProjectDTOId;
}
#OneToMany(mappedBy = "studentprojects", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
public Set<StudentProjectDTO> getUserRole() {
return this.studentProjectDTO;
}
public void setUserRole(Set<StudentProjectDTO> studentProjectDTO) {
this.studentProjectDTO = studentProjectDTO;
}
}
#Entity
#Table(name="studentprojects")
public class StudentProjectDTO {
private InitialProjectDTO project;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "initial_projectDTO_id", nullable = false)
public User getProject() {
return this.project;
}
public void setProject(InitialProjectDTO project) {
this.project = project;
}
}
your Query shoud be something like this
String hql="SELECT ip from InitialProjectDTO ip JOIN ip.studentProjectDTO sp WHERE sp.projectId = :id";
Query query=session.createQuery(hql);
query.setParameter("id", id);
mail =query.list();

Mapped Entity null on #OneToOne with #JoinColumn

I get the mapped entity always null but, FetchType.EAGER is set already. I have a Booking entity class that maps to two other entities - Slot and Subscriber. Both the entities are null when I fetch the booking entity
Booking class
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
#Entity
#Table(name = "BOOKING")
public class Booking {
public Booking(){
}
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#Column(name = "title")
private String title;
#Column(name = "descr")
private String desc;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "slotid",insertable = false, updatable = false)
private Slot slot;
private Integer slotid;
private Integer subscriberid;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "subscriberid",insertable = false, updatable = false)
private User subscriber;
#Column(name = "created")
#Temporal(TemporalType.TIMESTAMP)
private Date created;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "slotid",referencedColumnName="slotid")
public Slot getSlot() {
return slot;
}
public void setSlot(Slot slot) {
this.slot = slot;
}
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "subscriberid",referencedColumnName="userid")
public User getSubscriber() {
return subscriber;
}
public void setSubscriber(User subscriber) {
this.subscriber = subscriber;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Integer getSlotid() {
return slotid;
}
public void setSlotid(Integer slotid) {
this.slotid = slotid;
}
public Integer getSubscriberid() {
return subscriberid;
}
public void setSubscriberid(Integer subscriberid) {
this.subscriberid = subscriberid;
}
}
Slot class
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
#Entity
#Table(name="SLOT")
public class Slot {
public Slot(){
}
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="slotid")
private Integer id;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name="ownerid",insertable = false, updatable = false)
private User user;
#Column(name="startdate")
private Date startdate;
#Column(name="enddate")
private Date enddate;
#Column(name="status")
private String status;
private Integer ownerid;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "created", nullable = false, updatable=false)
#Version
private Date created;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return this.user;
}
public void setUser(User owner) {
this.user = owner;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getOwnerid() {
return ownerid;
}
public void setOwnerid(Integer ownerid) {
this.ownerid = ownerid;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getStartdate() {
return startdate;
}
public void setStartdate(Date startdate) {
this.startdate = startdate;
}
public Date getEnddate() {
return enddate;
}
public void setEnddate(Date enddate) {
this.enddate = enddate;
}
}
Subscriber - User class
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
#Entity
#Table(name="users")
public class User {
public User(){
}
#Id#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="userid")
private Integer userid = 0;
#Column(name = "name")
private String name;
#Column(name = "mobile")
private String mobile;
#Column(name = "password")
private String password;
#Column(name = "email")
private String email;
#Column(name = "type")
private String userType;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name="cityid",insertable = false, updatable = false)
private City city;
private String cityid;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name="specialityid",insertable = false, updatable = false)
private Speciality speciality;
private Integer specialityid;
#Column(name="medregno")
private String regno;
#Column(name="refcode")
private String referalcode;
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getReferalcode() {
return referalcode;
}
public void setReferalcode(String referalcode) {
this.referalcode = referalcode;
}
#Column(name = "gender")
private String gender;
#Column(name = "active")
private boolean active;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "updated")
private Date updated;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "created")
private Date created;
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (userid != other.userid)
return false;
return true;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public Speciality getSpeciality() {
return speciality;
}
public void setSpeciality(Speciality speciality) {
this.speciality = speciality;
}
public Integer getSpecialityid() {
return specialityid;
}
public void setSpecialityid(Integer specialityid) {
this.specialityid = specialityid;
}
}
booking.getSlot() and booking.getSubscriber() returns null
Please let me know if i miss some configuration while mapping
EDIT1
Added code how the entity is getting loaded
public Booking addBooking(String title,String desc,int slotid,int subscriberid,Session session){
Booking booking = new Booking();
booking.setTitle(title);
booking.setDesc(desc);
booking.setSlotid(slotid);
booking.setSubscriberid(subscriberid);
booking.setCreated(new Date());
Integer bookingid = (Integer) session.save(booking);
session.flush();
Booking bookingEntity = (Booking) session.createQuery("From Booking where id = ?").
setParameter(0, bookingid).list().get(0);
return bookingEntity;
}
I am saving the entity and reloading it.
It's not working because Hibernate is retuning the same instance it has already in its 1st level cache, which doesn't have a reference to any of the 2 other entities.
To fix this, you have to do a session.refresh(booking) rather than executing a query.
In your code :
booking.setSlotid(slotid);
booking.setSubscriberid(subscriberid);
You're just setting Integer values and not objects. Instead of this, try to set objects :
booking.setSlot(new Slot(slotid));
booking.setSubscriber(new Subscriber(subscriberid));
But as #Augusto said, the associations you're having in the session (Slot and Subscriber) are not full objects they contain only their ids. That's why you can't get other fields of these objects.

Categories