I've really been trying to solve this but whatever I try it is not working. Throughout other tables it has always worked but with the table I am currently making whatever I do a column that I want creating is never being created. I want a seasons_team table with a list of teams but after finishing the relationship in JPA the column never appears. Is there something wrong I'm doing?
Here is how I am creating the table
#Entity
public class SeasonFixtures {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int SeasonFixtureID;
#OneToOne
private Season seasonId;
#OneToMany(fetch = FetchType.LAZY)
#JoinColumn(name="season")
private List<Team> teams;
public int getSeasonid() {
return seasonid;
}
public void setSeasonid(int seasonid) {
this.seasonid = seasonid;
}
public String getSeasonName() {
return seasonName;
}
public void setSeasonName(String seasonName) {
this.seasonName = seasonName;
}
public SeasonTeams getSfid() {
return sfid;
}
public void setSfid(SeasonTeams sfid) {
this.sfid = sfid;
}
And here is the other end of the relationship
#Entity
public class Team {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int Team_Id;
private String teamName;
private Blob Logo;
private String stadium;
#OneToOne
private TotalTeamStats tts;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "SEASON_ID", referencedColumnName = "seasonid", unique=false)
private Season season;
public String getStadium() {
return stadium;
}
public void setStadium(String stadium) {
this.stadium = stadium;
}
public TotalTeamStats getTts() {
return tts;
}
public void setTts(TotalTeamStats tts) {
this.tts = tts;
}
public int getTeam_Id() {
return Team_Id;
}
public void setTeam_Id(int team_Id) {
Team_Id = team_Id;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Blob getLogo() {
return Logo;
}
public void setLogo(Blob blobs) {
Logo = blobs;
}
But this is a screenshot of my table in MYSQL
image of MYSQL
If anyone could help I would be greatly appreciative. I've been trying to fix this for ages. But I don't know where else to turn.
There is something wrong with your Annotations or your definition of the one to many relation. I could not extract how your one to many relation is defined, but i think its should look like that:
#ManyToOne
#JoinColumn(name="season")
private Season season;
#OneToMany(fetch = FetchType.LAZY)
private List<Team> teams;
I'm not 100% sure but i think in a one to many relation the #JoinColumn have to be at #ManytoOne site.
UPDATE
Maybe because you are using seasonId as Reference. Either Map the relation with the Season Entity or you maybe need to add the Column:
#OneToOne
private Season seasonId;
to your Table with:
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "seasonId")
private Season season;
Again just a thought.
so I have an hibernate Entity called Appointment, in this entity I have a AppointNumber property which itself contains a number property which is a string.
When I persist my Appointment, I need the AppointmentNumber. I got it to work with #Embedded and #Embeddable the other day but this creates a join table Which I can't have.
I tried many other solutions to try and get it to work without join tables but I can't figure it out. (I get lots of ava.lang.IllegalStateException)
Can anyone help?
Thanks!
#Entity(name = "appointments")
public class Appointment {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#OneToOne(mappedBy = "number")
#Fetch(value = FetchMode.SELECT)
private AppointmentNumber appointmentNumber;
Appointment entity
AppointmentNumber, used in Appointment but should not be an entity
public class AppointmentNumber {
#OneToOne
#JoinColumn(name = "appointmentNumber", unique = true, nullable = false)
private String number;
You could do like this:
#Entity(name = "appointments")
public class Appointment {
///....
#Convert(converter = AppointmentNumberConverter.class)
private AppointmentNumber appointmentNumber;
///....
}
#Converter
public class AppointmentNumberConverter implements
AttributeConverter<PersonName, String> {
#Override
public String convertToDatabaseColumn(AppointmentNumber appointmentNumber) {
if (appointmentNumber == null) {
return null;
}
return appointmentNumber.getNumber();
}
#Override
public AppointmentNumber convertToEntityAttribute(String appointmentNumber) {
if (appointmentNumber == null) {
return null;
}
AppointmentNumber result = new AppointmentNumber();
result.setNumber(appointmentNumber);
return result;
}
}
Have a look at JPA Converter documentation.
I have configured composite primary key for my entity Employee as follows
Employee.java:
#Entity
#Table(name="employee")
#Proxy(lazy=false)
#IdClass(EmployeeId.class)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private EmployeeId employeeId;
private Person person;
private Branch branch;
private boolean isActive;
public Employee() {
}
#EmbeddedId
#AttributeOverrides({
#AttributeOverride(name="person", column = #Column(name="person_id")),
#AttributeOverride(name="branch", column = #Column(name="branch_id"))})
public EmployeeId getEmployeeId() {
return employeeId;
}
public void setEmployeeId(EmployeeId employeeId) {
this.employeeId = employeeId;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="person_id")
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="branch_id")
public Branch getBranch() {
return branch;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
#Column(name="is_active")
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
}
EmployeeId.java:
#Embeddable
public class EmployeeId implements Serializable {
private static final long serialVersionUID = 1L;
private Person person;
private Branch branch;
public EmployeeId() {
}
public EmployeeId(Person argPerson, Branch argbranch) {
this.person = argPerson;
this.branch = argbranch;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="person_id", insertable=false, updatable=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="branch_id", insertable=false, updatable=false)
public Branch getBranch() {
return branch;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
}
I created a SessionFactory bean using class org.springframework.orm.hibernate5.LocalSessionFactoryBean and mapped all hbm.xml as a MappingLocations.
My code throws the following error:
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
at org.hibernate.metamodel.internal.AttributeFactory$3.resolveMember(AttributeFactory.java:971)
at org.hibernate.metamodel.internal.AttributeFactory$5.resolveMember(AttributeFactory.java:1029)
at org.hibernate.metamodel.internal.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:451)
at org.hibernate.metamodel.internal.AttributeFactory.buildIdAttribute(AttributeFactory.java:128)
at org.hibernate.metamodel.internal.MetadataContext.buildIdClassAttributes(MetadataContext.java:337)
at org.hibernate.metamodel.internal.MetadataContext.applyIdMetadata(MetadataContext.java:269)
at org.hibernate.metamodel.internal.MetadataContext.wrapUp(MetadataContext.java:190)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:219)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:296)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:476)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:707)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:504)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFac
How can I avoid this error? I am using spring-orm-4.3.1-RELEASE and hibernate-core-5.2.0.Final.
Update
I have created a sample project and I am getting the following error while running...
Caused by: org.hibernate.AnnotationException: Property of #IdClass not found in entity sample.domain.Employee: employee
Refer the code: https://www.dropbox.com/s/axr8l01iqh0qr29/idclass-using-hibernate5.tar.gz?dl=0
What I did wrong? Kindly provide your inputs here
Your situation corresponds to the chapter 2.4.1 Primary Keys Corresponding to Derived Identities of the JPA 2.1 Specification.
The identity of Employee is derived from identities of Person and Branch. You haven't shown the code of either of them, so I'll assume they have simple primary keys. In that relationship, Person and Branch are "parent entities" and Employee is a "dependant" entity.
The ID of Employee may be mapped using either IdClass or EmbeddedId, not both at the same time.
See chapter 2.4.1.1 Specification of Derived Identities.
If you want to use IdClass, then:
The names of the attributes of the id class and the Id attributes of the dependent entity class must correspond as follows:
The Id attribute in the entity class and the corresponding attribute in the id class must have the same name.
...
If an Id attribute in the entity is a many-to-one or one-to-one relationship to a parent entity, the corresponding attribute in the id class must be of (...) the type of the Id attribute of the parent entity.
So your classes would look like this (getters, setters, superfluous annotations etc. omitted)
#Entity
#IdClass(EmployeeId.class)
public class Employee {
#Id
#ManyToOne
private Person person;
#Id
#ManyToOne
private Branch branch;
}
public class EmployeeId {
private Long person; // Corresponds to the type of Person ID, name matches the name of Employee.person
private Long branch; // Corresponds to the type of Branch ID, name matches the name of Employee.branch
}
If you use EmbeddedId, then:
If the dependent entity uses an embedded id to represent its primary key, the attribute in the embedded id corresponding to the relationship attribute must be of the same type as the primary key of the parent entity and must be designated by the MapsId annotation applied to the relationship attribute. The value element of the MapsId annotation must be used to specify the name of the attribute within the embedded id to which the relationship attribute corresponds.
And the code would look like this:
#Entity
public class Employee {
#EmbeddedId
private EmployeeId id;
#ManyToOne
#MapsId("personId") // Corresponds to the name of EmployeeId.personId
private Person person;
#ManyToOne
#MapsId("branchId") // Corresponds to the name of EmployeeId.branchId
private Branch branch;
}
#Embeddable
public class EmployeeId {
private Long personId; // Corresponds to the type of Person ID
private Long branchId; // Corresponds to the type of Branch ID
}
A composite key mapping can be either done with an IdClass or an Embeddable. If you want to use an IdClass you have to annotate your fields in Employee with #Id.
#IdClass(EmployeeId.class)
class Person{
#Id
private Person person;
#Id
private Branch branch;
}
If you want to use an Embedded as a composite key please remove the #IdClass(EmployeeId.class) annotation from Person. You also don't need the person and branch field in your Person class because those are defined in your Embedded class.
Change to:
#Entity
#Table(name = "employee")
#Proxy(lazy = false)
#IdClass(EmployeeId.class)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private EmployeeId id;
private Person person;
private Branch branch;
private boolean isActive;
public Employee() {
}
#EmbeddedId
#AttributeOverrides({#AttributeOverride(name = "person", column = #Column(name = "person_id") ),
#AttributeOverride(name = "branch", column = #Column(name = "branch_id") )})
public EmployeeId getId() {
return id;
}
public void setId(EmployeeId id) {
this.id = id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "person_id")
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "branch_id")
public Branch getBranch() {
return branch;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
#Column(name = "is_active")
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
}
The IdClass shouldnt be defined as Embeddable -
#Entity
#Table(name="employee")
#IdClass(EmployeeId.class)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#ManyToOne
private Person person;
#Id
#ManyToOne
private Branch branch;
private boolean isActive;
public Employee() { }
//....
}
And -
public class EmployeeId implements Serializable {
private static final long serialVersionUID = 1L;
private Person person;
private Branch branch;
public EmployeeId() {}
public EmployeeId(Person argPerson, Branch argbranch) {
this.person = argPerson;
this.branch = argbranch;
}
}
Read your comment - Can I make a suggestion that you map Employee to person_id and branch_id, and not the JPA objects Person and Branch? This will let us test if your hbm config is correct. Id also suggest posting your hbm config as I think there is information missing from this problem
So the table will be similar to -
#Entity
#Table(name="employee")
#IdClass(EmployeeId.class)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private Long personId;
#Id
private Long branchId;
private boolean isActive;
public Employee() { }
//....
}
And -
And -
public class EmployeeId implements Serializable {
private static final long serialVersionUID = 1L;
private Long personId;
private Long branchId;
public EmployeeId() {}
public EmployeeId(Person argPerson, Branch argbranch) {
this.person = argPerson;
this.branch = argbranch;
}
}
This link could help you
JPA - EmbeddedId with #ManytoOne
Relationship mappings defined within an embedded id class are not supported.Then you need to change the embeddedId class like this
#Embeddable
public class EmployeeId implements Serializable {
private static final long serialVersionUID = 1L;
private Long personId;
private Long branchId;
public EmployeeId() {
}
public EmployeeId(Long argPerson, Long argbranch) {
this.personId = argPerson;
this.branchId = argbranch;
}
#Column(name = "person_id")
public Long getPersonId() {
return personId;
}
public void setPersonId(Long personId) {
this.personId = personId;
}
#Column(name = "branch_id")
public Long getBranchId() {
return branchId;
}
public void setBranchId(Long branchId) {
this.branchId = branchId;
}
}
JPA Composite Primary Key
Specifies a composite primary key class that is mapped to multiple fields or properties of the entity.
The names of the fields or properties in the primary key class and the
primary key fields or properties of the entity must correspond and
their types must be the same.
The answer is in here. read description for you. enter link description here
(Sample code)
#Entity
#Table(name = "EMP_PROJECT")
#IdClass(ProjectAssignmentId.class)
public class ProjectAssignment {
#Id
#Column(name = "EMP_ID", insertable = false, updatable = false)
private int empId;
#Id
#Column(name = "PROJECT_ID", insertable = false, updatable = false)
private int projectId;
#ManyToOne
#JoinColumn(name = "EMP_ID")
Professor employee;
#ManyToOne
#JoinColumn(name = "PROJECT_ID")
Project project;
....
}
public class ProjectAssignmentId implements Serializable {
private int empId;
private int projectId;
...
}
Mention #IdClass annotation with the class which holds the ID.
Check the answer at this post
I have a table like this:
Id int
Source VARCHAR
Target VARCHAR
And I want to query like this:
select * from `TestTable` t1
left join `TestTable` t2 on t1.`Target` = t2.`Target`
and t1.`Id` <> t2.`Id`
So how to code the mapping with java annotation?
And how to query with hibernate?
Mapping One to Many in hibernate with the same table is not a problem you can do with annotations like this:
#Entity
#Table(name = "test1")
public class Test1 implements Serializable {
private int id;
private String source;
private String target;
private List<Test1> others;
#Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#Column(name = "source")
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
#Column(name = "target")
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
#OneToMany(targetEntity = Test1.class)
public List<Test1> getOthers() {
return others;
}
public void setOthers(List<Test1> others) {
this.others = others;
}
}
The problem is the type of your relation because the relations not seems to be with the primary key. Then the relation is not strict "OneToMany". With the query like you write you can change the referencedColumnName in the JoinColumn annotation like this:
#JoinColumn(name = "target", referencedColumnName = "target")
But with this mapping, if the parent object has the same "target" field then the parent object is included as child because you cannot put the other part of the query in the mapping
t1.`Id` <> t2.`Id`
I'm working on a project where I need to save a download file information with keywords.
Typical one-to-many scenario, right? A download record to many download-keyword relationship records.
So here is what the Download.java look like
#Entity
#Table(name = "downloads", catalog = "nbpx")
public class Download extends BaseEntity implements Serializable {
public Integer downloadId;
public String title;
public Set<DownloadKeyword> downloadKeywords = new HashSet<DownloadKeyword>(
0);
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "downloadId", unique = true, nullable = false)
public Integer getDownloadId() {
return downloadId;
}
public void setDownloadId(Integer downloadId) {
this.downloadId = downloadId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#OneToMany(fetch = FetchType.EAGER, mappedBy = "download")
#Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
public Set<DownloadKeyword> getDownloadKeywords() {
return downloadKeywords;
}
public void setDownloadKeywords(Set<DownloadKeyword> downloadKeywords) {
this.downloadKeywords = downloadKeywords;
}
}
Since I don't want any duplicate download-keyword relation records in the table, I set a JPA unique Constraints for the class. But I didn't use a composite key. And this is what DownloadKeyword.java look like:
#Entity
#Table(name = "downloadkeywords", catalog = "nbpx", uniqueConstraints =
#UniqueConstraint(columnNames = {"keywordId", "downloadId" }))
public class DownloadKeyword extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
public Integer downloadKeywordId;
public Download download;
public Integer keywordId;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "downloadKeywordId", unique = true, nullable = false)
public Integer getDownloadKeywordId() {
return downloadKeywordId;
}
public void setDownloadKeywordId(Integer downloadKeywordId) {
this.downloadKeywordId = downloadKeywordId;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "downloadId", nullable = false, insertable=true)
public Download getDownload() {
return download;
}
public void setDownload(Download download) {
this.download = download;
}
public Integer getKeywordId() {
return keywordId;
}
public void setKeywordId(Integer keywordId) {
this.keywordId = keywordId;
}
}
When I save a download entity, I expect to see all the download-keyword relationship entities are saved within the download entity save session. And I need to avoid duplication in download-keyword relationship table. But every time I save it, the relation records were inserted straightly into DB without checking the duplication with JPA annotation.
So here is my question: Did multiple-column uniqueConstraints of JPA annotation get ignored when entities were saving in one-to-many save or update session?
The uniqueConstraints are only for table generation support.
See the documentation.
Since you're already using Set<DownloadKeyword> in the Download class - you can implement hashCode and equals methods in the DownloadKeyword class (include primary key downloadKeywordId and the keywordId properties) to accomplish what you want.
However, I would still prefer a composite key approach.