I'm using a Derby database and Hibernate to pull table columns into Java class fields. When pulling data for a class called CourseCredits.class
, I get a MappingException. This is because the column with name Course is of type VARCHAR(30)
and the corresponding class field is of type Course.class
:
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@Column(name="Course")
private Course course;
public CourseCredit(){
}
// getters and setters
}
If I change the global variable course
to a String type, then the program works as intended:
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@Column(name="Course")
private String course;
...
}
Given that the class Course.class
looks like this:
public class Course {
private String name;
...
}
Is it possible to store the String from the course column into an instance of Course.class
, in particular to the global variable name
?
Following the suggestion of Bartosz, the classes now look like this:
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@OneToOne
@JoinColumn(name = "CourseId")
private Course course;
...
}
@Entity
public class Course implements Serializable{
@Id
@Column(name="CourseId")
private String name;
...
}
I made an entity out of Course.class
since the association mapping specifies relations between entities. Unfortunately, the error is not solved yet. Now I get an error ERROR 42X04
. This is what I run:
em.getTransaction().begin(); // em is the entity manager
List<CourseCredit> credits = this.em.createQuery("from CourseCredit").getResultList();
The problem does not lie in this execution code, as the program works fine if I use a field of type String instead of Course
. The error output I get is:
Caused by: java.sql.SQLSyntaxErrorException: Column 'COURSECRED0_.COURSEID' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'COURSECRED0_.COURSEID' is not a column in the target table.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
... 19 more
It seems to complain about a non-existing column (CourseId
), which would be true. I'm not knowledgeable about databases at all, so forgive me if it's a newbie question, but what is the significance of the name CourseId
in @JoinColumn
? It seems to denote a new column name. I see it used in every example I can find, but it is never referenced to after defining it.
Associations are used for that.
You need to decide about cardinality (one-to-one? many-to-many?)
Assuming one to one and your case you need
@OneToOne
@JoinColumn(name = "course_id")
private Course course;
When using associations, linked table (curse in your case) must exist. you may create it manually or generate db schema from Hibernate annotations.
Hibernate documentation has examples for all the cases. Documentation includes underlying table structure.
See for more details http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations