Once you have moved properties to the embeddable class you need to remove them from the main class, there will be just one reference of your embedded class, so in your example you need to remove all properties like location from your MediaLocator class.
Up vote 0 down vote favorite share g+ share fb share tw.
I want to create a table, where the id is come strings composed. This is how I did is following some examples and documentation: @Entity @Table(name = "media_locator") public class MediaLocator { private List mediaObjects; private MediaLocatorPK primaryKey = new MediaLocatorPK(); // @Id // @GeneratedValue(strategy = GenerationType. AUTO) // @Column(name = "id", unique = true, nullable = false) // public int getId() { // return id; // } // // public void setId(int id) { // this.
Id = id; // } @EmbeddedId public MediaLocatorPK getPrimaryKey() { return primaryKey; } public void setPrimaryKey(MediaLocatorPK primaryKey) { this. PrimaryKey = primaryKey; } // @Column(name = "location", length = 200) public String getLocation() { return primaryKey.getLocation(); } public void setLocation(String location) { this.primaryKey. SetLocation(location); } // @Column(name = "description", length = 200, nullable = false) public String getDescription() { return primaryKey.getDescription(); } public void setDescription(String description) { this.primaryKey.
SetDescription(description); } // @OneToMany(cascade=CascadeType. ALL, fetch=FetchType. LAZY, mappedBy="mediaLocator") // public List getMediaObjects() { // return mediaObjects; // } // // public void setMediaObjects(List mediaObjects) { // mediaObjects = mediaObjects; // } @ManyToMany( cascade={CascadeType.
ALL}, fetch=FetchType. LAZY) @JoinTable(name = "media_object_location", joinColumns=@JoinColumn(name="mediaLocator_id"), inverseJoinColumns=@JoinColumn(name="mediaObject_id")) public List getMediaObjects() { return mediaObjects; } public void setMediaObjects(List mediaObjects) { this. MediaObjects = mediaObjects; } // @Column(name = "protocol", length = 200, nullable = false) public String getProtocol() { return primaryKey.getProtocol(); } public void setProtocol(String protocol) { this.primaryKey.
SetProtocol(protocol); } // @Column(name = "host", length = 200, nullable = false) public String getHost() { return primaryKey.getHost(); } public void setHost(String host) { this.primaryKey. SetHost(host); } // @Column(name = "port", length = 200, nullable = false) public String getPort() { return primaryKey.getPort(); } public void setPort(String port) { this.primaryKey. SetPort(port); } // @Column(name = "path", length = 200, nullable = false) public String getPath() { return primaryKey.getPath(); } public void setPath(String path) { this.primaryKey.
SetPath(path); } @Embeddable class MediaLocatorPK implements Serializable { private String location; private String description; private String protocol; private String host; private String port; private String path; public String getLocation() { return location; } public void setLocation(String location) { this. Location = location; } public String getDescription() { return description; } public void setDescription(String description) { this. Description = description; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.
Protocol = protocol; } public String getHost() { return host; } public void setHost(String host) { this. Host = host; } public String getPort() { return port; } public void setPort(String port) { this. Port = port; } public String getPath() { return path; } public void setPath(String path) { this.
Path = path; } } Before, I had an id auto generated, and each atribute had a colum in the table, @Column(name = "location", length = 200), for instance. In this way I can not store the data due to referencies from relations with other objects Caused by: org.hibernate. AnnotationException: A Foreign key refering com.app.
MediaLocator from com.app. MediaObject has the wrong number of column. Should be 6 at org.hibernate.cfg.annotations.TableBinder.
BindFk(TableBinder. Java:429) at org.hibernate.cfg.annotations. CollectionBinder.
BindCollectionSecondPass(CollectionBinder. Java:1443) at org.hibernate.cfg.annotations. CollectionBinder.
BindManyToManySecondPass(CollectionBinder. Java:1262) at org.hibernate.cfg.annotations. CollectionBinder.
BindStarToManySecondPass(CollectionBinder. Java:693) at org.hibernate.cfg.annotations. CollectionBinder$1.
SecondPass(CollectionBinder. Java:628) at org.hibernate.cfg. CollectionSecondPass.
DoSecondPass(CollectionSecondPass. Java:65) at org.hibernate.cfg.Configuration. OriginalSecondPassCompile(Configuration.
Java:1686) at org.hibernate.cfg.Configuration. SecondPassCompile(Configuration. Java:1393) at org.hibernate.cfg.Configuration.
BuildMappings(Configuration. Java:1345) at org.springframework.orm. Hibernate3.
LocalSessionFactoryBean. BuildSessionFactory(LocalSessionFactoryBean. Java:717) at org.springframework.orm.
Hibernate3. AbstractSessionFactoryBean. AfterPropertiesSet(AbstractSessionFactoryBean.
Java:211) at org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory. InvokeInitMethods(AbstractAutowireCapableBeanFactory.
Java:1469) at org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory. InitializeBean(AbstractAutowireCapableBeanFactory.
Java:1409) This specific relation is: @ManyToMany( cascade={CascadeType. ALL}, fetch=FetchType. LAZY) @JoinTable(name = "media_object_location", joinColumns=@JoinColumn(name="mediaLocator_id"), inverseJoinColumns=@JoinColumn(name="mediaObject_id")) public List getMediaObjects() { return mediaObjects; } public void setMediaObjects(List mediaObjects) { this.
MediaObjects = mediaObjects; } And from mediaObject side: @ManyToMany(cascade = { CascadeType. PERSIST, CascadeType. MERGE }, fetch = FetchType.
LAZY, mappedBy = "mediaObjects") public List getMediaLocators() { return mediaLocators; } public void setMediaLocators(List mediaLocators) { this. MediaLocators = mediaLocators; } what am I doing wrong? Thanks in advance hibernate link|improve this question edited Dec 14 '11 at 18:57 asked Dec 14 '11 at 18:19Blanca Hdez439419 90% accept rate.
– mprabhat Dec 14 '11 at 18:31 just edited my question, I realized I had no enough informaiton – Blanca Hdez Dec 14 '11 at 18:32 You went from a good design (single ID column, autogenerated) to a bad one (a table where all the columns form a composite primary key). Why are you doing that? Every entity referencing a MediaLocator object will have to duplicate the 6 columns in its own table.
And you won't be able to change any value in a MediaLocator. Don't do that! – JB Nizet Dec 14 '11 at 18:52 @JB Nizet because I need all the properties to be the id, in order to have only once the values.
With the id autogenerated, I had to check everythime whether that location exist or not. – Blanca Hdez Dec 14 '11 at 19:00 Then keep the ID as is, and just use a unique constraint on all the columns. That's the good way to do it.
I'll repeat: if you make the 6 columns the primary key, all these columns will have to be repeated to every table having a foreign key to MediaLocator, and you won't be able to modify a MediaLocator anymore. – JB Nizet Dec 14 '11 at 19:04.
Once you have moved properties to the embeddable class you need to remove them from the main class, there will be just one reference of your embedded class, so in your example you need to remove all properties like location from your MediaLocator class. Also what is property id doing in your MediaLocator? Since there is no specific column annotation in your class hibernate will try to map each and every attribute to the table column, it will succeed in that mapping if the naming of both the property and table column are same.
If you don't need id then either remove it or mark it as @Transient. This piece of code will reproduce the same error what you are getting. Student Class package com.mumz.test.jpa.embedded.
Manytomany; import java.util. Set; import javax.persistence. CascadeType; import javax.persistence.
Entity; import javax.persistence. Id; import javax.persistence. JoinColumn; import javax.persistence.
JoinTable; import javax.persistence. ManyToMany; import javax.persistence. Table; @Entity @Table(name="STUDENT") public class StudentMTM { private StudentMTMPK studentPK = null; private Set address = null; public StudentMTM(StudentMTMPK studentPK, Set address) { super(); this.
StudentPK = studentPK; this. Address = address; } /** * @return the id */ public Integer getId() { return studentPK.getId(); } /** * @param id the id to set */ public void setId(Integer id) { studentPK. SetId(id); } /** * @return the name */ public String getName() { return studentPK.getName(); } /** * @param name the name to set */ public void setName(String name) { studentPK.
SetName(name); } /** * @param studentPK * the studentPK to set */ public void setStudentPK(StudentMTMPK studentPK) { this. StudentPK = studentPK; } /** * @param address * the address to set */ public void setAddress(Set address) { this. Address = address; } /** * @return the address */ @ManyToMany(cascade = CascadeType.
ALL) @JoinTable(name = "STUDENT_ADDRESS", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "ADDRESS_ID") }) public Set getAddress() { return address; } /** * @return the studentPK */ @Id public StudentMTMPK getStudentPK() { return studentPK; } /* * (non-Javadoc) * * @see java.lang. Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((studentPK == null)? 0 : studentPK.hashCode()); result = prime * result + ((address == null)?
0 : address.hashCode()); return result; } /* * (non-Javadoc) * * @see java.lang. Object#equals(java.lang. Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof StudentMTM)) { return false; } StudentMTM other = (StudentMTM) obj; if (studentPK == null) { if (other.
StudentPK! = null) { return false; } } else if (!studentPK. Equals(other.
StudentPK)) { return false; } if (address == null) { if (other. Address! = null) { return false; } } else if (!address.
Equals(other. Address)) { return false; } return true; } } Student PK Class: package com.mumz.test.jpa.embedded. Manytomany; import java.io.
Serializable; import javax.persistence. Column; import javax.persistence. Embeddable; @Embeddable public class StudentMTMPK implements Serializable{ /** * */ private static final long serialVersionUID = 3686950547855931594L; private Integer id = null; private String name = null; public StudentMTMPK(Integer id, String name) { this.
Id = id; this. Name = name; } /** * @return the id */ @Column(name="STUDENT_ID") public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this. Id = id; } /** * @return the name */ @Column(name="STUDENT_NAME") public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.
Name = name; } /* (non-Javadoc) * @see java.lang. Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null)? 0 : id.hashCode()); result = prime * result + ((name == null)?
0 : name.hashCode()); return result; } /* (non-Javadoc) * @see java.lang. Object#equals(java.lang. Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof StudentMTMPK)) { return false; } StudentMTMPK other = (StudentMTMPK) obj; if (id == null) { if (other.
Id! = null) { return false; } } else if (!id. Equals(other.
Id)) { return false; } if (name == null) { if (other. Name! = null) { return false; } } else if (!name.
Equals(other. Name)) { return false; } return true; } } Address Class package com.mumz.test.jpa.embedded. Manytomany; import javax.persistence.
Column; import javax.persistence. Entity; import javax.persistence. GeneratedValue; import javax.persistence.
GenerationType; import javax.persistence. Id; import javax.persistence. Table; @Entity @Table(name="ADDRESS") public class Address { private Integer id = null; private String addressDetails = null; /** * @return the id */ @Id @GeneratedValue(strategy=GenerationType.
AUTO) @Column(name="ADDRESS_ID") public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this. Id = id; } /** * @return the addressDetails */ @Column(name="ADDRESS_DETAILS") public String getAddressDetails() { return addressDetails; } /** * @param addressDetails the addressDetails to set */ public void setAddressDetails(String addressDetails) { this. AddressDetails = addressDetails; } /* (non-Javadoc) * @see java.lang.
Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((addressDetails == null)? 0 : addressDetails.hashCode()); result = prime * result + ((id == null)? 0 : id.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.
Object#equals(java.lang. Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Address)) { return false; } Address other = (Address) obj; if (addressDetails == null) { if (other. AddressDetails!
= null) { return false; } } else if (!addressDetails. Equals(other. AddressDetails)) { return false; } if (id == null) { if (other.
Id! = null) { return false; } } else if (!id. Equals(other.
Id)) { return false; } return true; } } If you run this piece of code and try to save an instance of Address you will get Exception in thread "main" javax.persistence. PersistenceException: PersistenceUnit: myJPAService Unable to configure EntityManagerFactory at org.hibernate.ejb. Ejb3Configuration.
Configure(Ejb3Configuration. Java:378) at org.hibernate.ejb. HibernatePersistence.
CreateEntityManagerFactory(HibernatePersistence. Java:56) at javax.persistence.Persistence. CreateEntityManagerFactory(Persistence.
Java:63) at javax.persistence.Persistence. CreateEntityManagerFactory(Persistence. Java:47) at com.mumz.test.jpa.embedded.manytomany.
EmbeddableTestMainApp. Main(EmbeddableTestMainApp. Java:9) Caused by: org.hibernate.
AnnotationException: A Foreign key refering com.mumz.test.jpa.embedded.manytomany. StudentMTM from com.mumz.test.jpa.embedded.manytomany. Address has the wrong number of column.
Should be 2 at org.hibernate.cfg.annotations.TableBinder. BindFk(TableBinder. Java:429) at org.hibernate.cfg.annotations.
CollectionBinder. BindCollectionSecondPass(CollectionBinder. Java:1460) at org.hibernate.cfg.annotations.
CollectionBinder. BindManyToManySecondPass(CollectionBinder. Java:1279) at org.hibernate.cfg.annotations.
CollectionBinder. BindStarToManySecondPass(CollectionBinder. Java:710) at org.hibernate.cfg.annotations.
CollectionBinder$1. SecondPass(CollectionBinder. Java:645) at org.hibernate.cfg.
CollectionSecondPass. DoSecondPass(CollectionSecondPass. Java:65) at org.hibernate.cfg.Configuration.
OriginalSecondPassCompile(Configuration. Java:1716) at org.hibernate.cfg.Configuration. SecondPassCompile(Configuration.
Java:1423) at org.hibernate.cfg.Configuration. BuildMappings(Configuration. Java:1375) at org.hibernate.ejb.
Ejb3Configuration. BuildMappings(Ejb3Configuration. Java:1519) at org.hibernate.ejb.
EventListenerConfigurator. Configure(EventListenerConfigurator. Java:193) at org.hibernate.ejb.
Ejb3Configuration. Configure(Ejb3Configuration. Java:1100) at org.hibernate.ejb.
Ejb3Configuration. Configure(Ejb3Configuration. Java:282) at org.hibernate.ejb.
Ejb3Configuration. Configure(Ejb3Configuration. Java:366) My manytomany in StudentMTM is broken.
Now this piece will work as it should be: Student Class package com.mumz.test.jpa.embedded. Manytomany; import java.util. Set; 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.
ManyToMany; import javax.persistence. Table; @Entity @Table(name="STUDENT") public class StudentMTM { private StudentMTMPK studentPK = null; private Integer id = null; private Set address = null; public StudentMTM(StudentMTMPK studentPK, Set address) { super(); this. StudentPK = studentPK; this.
Address = address; } /** * @return the id */ @Column(name="STUDENT_ID") public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this. Id = id; } /** * @return the name */ public String getName() { return studentPK.getName(); } /** * @param name the name to set */ public void setName(String name) { studentPK. SetName(name); } /** * @param studentPK * the studentPK to set */ public void setStudentPK(StudentMTMPK studentPK) { this.
StudentPK = studentPK; } /** * @param address * the address to set */ public void setAddress(Set address) { this. Address = address; } /** * @return the address */ @ManyToMany(cascade = CascadeType. ALL) @JoinTable(name = "STUDENT_ADDRESS", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "ADDRESS_ID") }) public Set getAddress() { return address; } /** * @return the studentPK */ @Id public StudentMTMPK getStudentPK() { return studentPK; } /* * (non-Javadoc) * * @see java.lang.
Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((studentPK == null)? 0 : studentPK.hashCode()); result = prime * result + ((address == null)? 0 : address.hashCode()); return result; } /* * (non-Javadoc) * * @see java.lang.
Object#equals(java.lang. Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof StudentMTM)) { return false; } StudentMTM other = (StudentMTM) obj; if (studentPK == null) { if (other. StudentPK!
= null) { return false; } } else if (!studentPK. Equals(other. StudentPK)) { return false; } if (address == null) { if (other.
Address! = null) { return false; } } else if (!address. Equals(other.
Address)) { return false; } return true; } } Student PK package com.mumz.test.jpa.embedded. Manytomany; import java.io. Serializable; import javax.persistence.
Column; import javax.persistence. Embeddable; @Embeddable public class StudentMTMPK implements Serializable{ /** * */ private static final long serialVersionUID = 3686950547855931594L; private String name = null; public StudentMTMPK(String name) { this. Name = name; } /** * @return the name */ @Column(name="STUDENT_NAME") public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.
Name = name; } /* (non-Javadoc) * @see java.lang. Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null)? 0 : name.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.
Object#equals(java.lang. Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof StudentMTMPK)) { return false; } StudentMTMPK other = (StudentMTMPK) obj; if (name == null) { if (other. Name!
= null) { return false; } } else if (!name. Equals(other. Name)) { return false; } return true; } } Address Class - No change from the first version.
In the second version JoinColumn is part of Student and mapping knows where it is mapping to.
I want the id to be all the properties combined. I have just edited my question with the changes in my code. Thanks – Blanca Hdez Dec 14 '11 at 18:58 with that your association manytomany is broken, your join column is mediaLocator_id so which column in your Media Locator it is going to refer to?
None. – mprabhat Dec 14 '11 at 20:38 private Integer id = null; In Student Class. How will you generate that id.
Autogenerated? Then, what's the difference between this second model and the first one? – Blanca Hdez Dec 15 '11 at 7:28 nope I am creating the id in my my main class where I am creating the entity... I didn't post that code as it will just get too blotted.. – mprabhat Dec 15 '11 at 10:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.