Hi, I have a problem with my code and don't understand where is the problem.
I want to use this for a database of 3 tables :
Code:
CREATE TABLE `algorithmen` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(20) default NULL,
PRIMARY KEY (`Id`)
)
CREATE TABLE `attribute` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(20) default NULL,
`Unit` varchar(20) default NULL,
PRIMARY KEY (`Id`)
)
CREATE TABLE `algoattribute` (
`IdAttribute` int(11) NOT NULL default '0',
`IdAlgorithmen` int(11) NOT NULL default '0',
`OrdnungIndex` int(11) default NULL,
PRIMARY KEY (`IdAttribute`,`IdAlgorithmen`),
CONSTRAINT `algoattribute_ibfk_1` FOREIGN KEY (`IdAttribute`) REFERENCES `attribute` (`Id`),
CONSTRAINT `algoattribute_ibfk_2` FOREIGN KEY (`IdAlgorithmen`) REFERENCES `algorithmen` (`Id`)
)
Here is my code :
For the table "Attribut" :
Code:
@Entity
@Table(name = "attribute", catalog = "algoattribut")
@SuppressWarnings("serial")
public class Attribute implements Serializable {
/**
* Attribute id.
*/
private Integer id;
/**
* Attribute name.
*/
private String name;
/**
* Attribute unit.
*/
private String unit;
/**
* List of Algoattribute
*/
private List<Algoattribute> algoattributes = null;
/**
* <p>
* </p>
* @return id
*/
@Basic
@Id
@GeneratedValue
@Column(name = "Id")
public Integer getId() {
return id;
}
/**
* @param id new value for id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return name
*/
@Basic
@Column(name = "Name", length = 20)
public String getName() {
return name;
}
/**
* @param name new value for name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return unit
*/
@Basic
@Column(name = "Unit", length = 20)
public String getUnit() {
return unit;
}
/**
* @param unit new value for unit
*/
public void setUnit(String unit) {
this.unit = unit;
}
/**
* Get the list of Algoattribute
*/
[b] @OneToMany(mappedBy="attribute")[/b]
public List<Algoattribute> getAlgoattributes() {
return this.algoattributes;
}
/**
* Set the list of Algoattribute
*/
public void setAlgoattributes(List<Algoattribute> algoattributes) {
this.algoattributes = algoattributes;
}
}
The code for the class Algorithmen is very similar, and this is the code for the class AlgoAttribut :
Code:
@Entity
@Table(name = "algoattribute", catalog = "algoattribut")
@SuppressWarnings("serial")
public class Algoattribute implements Serializable {
/**
* Primary key
*/
private AlgoattributePK algoattributePK;
/**
* Attribute ordnungIndex.
*/
private Integer ordnungIndex;
/**
* Get the primary key
*/
@Basic
@Id
public AlgoattributePK getAlgoattributePK() {
return this.algoattributePK;
}
/**
* set the primary key
*/
public void setAlgoattributePK(AlgoattributePK algoattributePK) {
this.algoattributePK = algoattributePK;
}
/**
* @return ordnungIndex
*/
@Basic
@Column(name = "OrdnungIndex")
public Integer getOrdnungIndex() {
return ordnungIndex;
}
/**
* @param ordnungIndex new value for ordnungIndex
*/
public void setOrdnungIndex(Integer ordnungIndex) {
this.ordnungIndex = ordnungIndex;
}
/**
*
*/
@SuppressWarnings("serial")
@Embeddable
public static class AlgoattributePK implements Serializable {
/**
* Attribute attribute
*/
private Attribute attribute;
/**
* Attribute algorithmen
*/
private Algorithmen algorithmen;
/**
* get attribute
*/
[b]@ManyToOne
@JoinColumn(name = "IdAttribute")[/b]
public Attribute getAttribute() {
return this.attribute;
}
/**
* set attribute
*/
public void setAttribute(Attribute attribute) {
this.attribute = attribute;
}
/**
* get algorithmen
*/
[b]@ManyToOne
@JoinColumn(name = "IdAlgorithmen")[/b]
public Algorithmen getAlgorithmen() {
return this.algorithmen;
}
/**
* set algorithmen
*/
public void setAlgorithmen(Algorithmen algorithmen) {
this.algorithmen = algorithmen;
}
/**
* calculate hashcode
*/
@Override
public int hashCode()
{
//TODO : implement this method
return super.hashCode();
}
/**
* equals method
*/
@Override
public boolean equals(Object object)
{
//TODO : implement this method
return super.equals(object);
}
}
}
And I have this problem :
Quote:
Exception in thread "main" java.lang.ExceptionInInitializerError
at pack.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:22)
at Test.main(Test.java:16)
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: pack.pojo.Algoattribute.algorithmen in pack.pojo.Algorithmen.algoattributes
And I don't find out from where the problem comes from...