问题
I am trying to test one to many mapping by creating tables from domain objects but I see the error mappedBy reference an unknown target entity property. Could some one take a look please?
Thanks
Employee.java
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "employeetype", discriminatorType
=DiscriminatorType.STRING)
@Table(name = "Employee")
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "status")
private EmployeeStatus status;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "hireDate")
private DateTime hireDate;
/**
* @return the hireDate
*/
public DateTime getHireDate() {
return hireDate;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the Status
*/
public EmployeeStatus getStatus() {
return status;
}
/**
* @param hireDate
* the hireDate to set
*/
public void setHireDate(final DateTime hireDate) {
this.hireDate = hireDate;
}
/**
* @param id
* the id to set
*/
public void setId(final int id) {
this.id = id;
}
/**
* @param name
* the name to set
*/
public void setName(final String name) {
this.name = name;
}
/**
* @param status
* the status to set
*/
public void setStatus(final EmployeeStatus status) {
this.status = status;
}
}
FulltimeEmployee.java
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@DiscriminatorValue("FulltimeEmployee")
@Table(name = "FulltimeEmployee")
public class FulltimeEmployee extends Employee {
@Column(name = "cubeNumber")
private String cubeNumber;
@ManyToOne
@JoinColumn(name = "id")
private FlightBenefit flightBenefit;
/**
* @return the cubeNumber
*/
public String getCubeNumber() {
return cubeNumber;
}
/**
* @return the flightBenefit
*/
public FlightBenefit getFlightBenefit() {
return flightBenefit;
}
/**
* @param cubeNumber
* the cubeNumber to set
*/
public void setCubeNumber(final String cubeNumber) {
this.cubeNumber = cubeNumber;
}
/**
* @param flightBenefit
* the flightBenefit to set
*/
public void setFlightBenefit(final FlightBenefit flightBenefit) {
this.flightBenefit = flightBenefit;
}
}
ContractEmployee.java
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@DiscriminatorValue("ContractEmployee")
@Table(name = "ContractEmployee")
public class ContractEmployee extends Employee {
@Column(name = "openAreaNumber")
private String openAreaNumber;
/**
* @return the openAreaNumber
*/
public String getOpenAreaNumber() {
return openAreaNumber;
}
/**
* @param openAreaNumber
* the openAreaNumber to set
*/
public void setOpenAreaNumber(final String openAreaNumber) {
this.openAreaNumber = openAreaNumber;
}
}
FlightBenefit.java
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
@Entity
@Table(name = "FlightBenefit")
public class FlightBenefit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private String id;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "useByTime")
private DateTime useByTime;
@Column(name = "discountAmount")
private String discountAmount;
@OneToMany(mappedBy = "FlightBenefit")
private Set<FulltimeEmployee> FulltimeEmployees;
/**
* @return the discountAmount
*/
public String getDiscountAmount() {
return discountAmount;
}
/**
* @return the fulltimeEmployees
*/
public Set<FulltimeEmployee> getFulltimeEmployees() {
return FulltimeEmployees;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @return the useByTime
*/
public DateTime getUseByTime() {
return useByTime;
}
/**
* @param discountAmount
* the discountAmount to set
*/
public void setDiscountAmount(final String discountAmount) {
this.discountAmount = discountAmount;
}
/**
* @param fulltimeEmployees
* the fulltimeEmployees to set
*/
public void setFulltimeEmployees(final Set<FulltimeEmployee> fulltimeEmployees) {
FulltimeEmployees = fulltimeEmployees;
}
/**
* @param id
* the id to set
*/
public void setId(final String id) {
this.id = id;
}
/**
* @param useByTime
* the useByTime to set
*/
public void setUseByTime(final DateTime useByTime) {
this.useByTime = useByTime;
}
}
EmployeeStatus
public enum EmployeeStatus {
ACTIVE,
INACTIVE
}
hibernate.cfg.xml
?xml version="1.0" encoding="UTF-8"?
!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
hibernate-configuration
session-factory
property name="connection.driver_class">com.mysql.jdbc.Driver</property
property
name="connection.url">jdbc:mysql://localhost:3306/TestDB</property
property name="connection.username">user</property
property name="connection.password">password123</property
property name="show_sql">true</property
property name="dialect">org.hibernate.dialect.MySQLDialect</property
property name="hibernate.default_schema">TestSchema</property
!-- <property name="hbm2ddl.auto">validate</property> --
/session-factory&
/hibernate-configuration&
**Main.java**
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.joda.time.DateTime;
public class Main {
public static void main(final String args[]) {
Configuration config = new Configuration();
config.addAnnotatedClass(ContractEmployee.class);
config.addAnnotatedClass(FulltimeEmployee.class);
config.addAnnotatedClass(FlightBenefit.class);
config.configure("hibernate.cfg.xml");
SchemaExport se = new SchemaExport(config);
se.execute(true, true, false, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
ContractEmployee cEmployee = new ContractEmployee();
cEmployee.setName("Raj");
cEmployee.setStatus(EmployeeStatus.ACTIVE);
cEmployee.setHireDate(DateTime.now());
cEmployee.setOpenAreaNumber("421a29");
session.save(cEmployee);
FlightBenefit flightbenefit = new FlightBenefit();
flightbenefit.setDiscountAmount("1000");
flightbenefit.setUseByTime(DateTime.now());
FulltimeEmployee fEmployee = new FulltimeEmployee();
fEmployee.setName("Teja");
fEmployee.setStatus(EmployeeStatus.INACTIVE);
fEmployee.setHireDate(DateTime.now());
fEmployee.setCubeNumber("Cube 19");
fEmployee.setFlightBenefit(flightbenefit);
session.save(fEmployee);
session.flush();
session.getTransaction().commit();
session.close();
}
}
Error StackTrace
Jun 1, 2013 2:21:14 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jun 1, 2013 2:21:14 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jun 1, 2013 2:21:14 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 1, 2013 2:21:14 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 1, 2013 2:21:14 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Jun 1, 2013 2:21:14 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Jun 1, 2013 2:21:14 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 1, 2013 2:21:14 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jun 1, 2013 2:21:15 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy:
ContractEmployee
Jun 1, 2013 2:21:15 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy:
FulltimeEmployee
Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an
unknown target entity property: FulltimeEmployee.FlightBenefit in
FlightBenefit.FulltimeEmployees at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:708)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1611)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1369)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:941)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:188)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:156)
at Main.main(Main.java:23)
Thanks for ur reply. I made the changes you have mentioned and also made few other changes like adding session.save(flightBenefit) to Main.java and insertable = false, updatable = false with @JoinColumn annotation. I see Employee table being created but not FlightBenefit and am seeing the error
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TestSchema.flightbenefit' doesn't exist
Below are the classes with updated changes and stacktrace
Main.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.joda.time.DateTime;
public class Main {
public static void main(final String args[]) {
Configuration config = new Configuration();
config.addAnnotatedClass(ContractEmployee.class);
config.addAnnotatedClass(FulltimeEmployee.class);
config.addAnnotatedClass(FlightBenefit.class);
config.configure("hibernate.cfg.xml");
SchemaExport se = new SchemaExport(config);
se.execute(true, true, false, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
FlightBenefit flightbenefit = new FlightBenefit();
flightbenefit.setDiscountAmount("1000");
flightbenefit.setUseByTime(DateTime.now());
session.save(flightbenefit);
ContractEmployee cEmployee = new ContractEmployee();
cEmployee.setName("Raj");
cEmployee.setStatus(EmployeeStatus.ACTIVE);
cEmployee.setHireDate(DateTime.now());
cEmployee.setOpenAreaNumber("421a29");
session.save(cEmployee);
FulltimeEmployee fEmployee = new FulltimeEmployee();
fEmployee.setName("Teja");
fEmployee.setStatus(EmployeeStatus.INACTIVE);
fEmployee.setHireDate(DateTime.now());
fEmployee.setCubeNumber("Cube 19");
fEmployee.setFlightBenefit(flightbenefit);
session.save(fEmployee);
session.flush();
session.getTransaction().commit();
session.close();
}
}
FulltimeEmployee.Java
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
@DiscriminatorValue("FulltimeEmployee")
public class FulltimeEmployee extends Employee {
@Column(name = "cubeNumber")
private String cubeNumber;
@ManyToOne
@JoinColumn(name = "id", insertable = false, updatable = false)
private FlightBenefit flightBenefit;
/**
* @return the cubeNumber
*/
public String getCubeNumber() {
return cubeNumber;
}
/**
* @return the flightBenefit
*/
public FlightBenefit getFlightBenefit() {
return flightBenefit;
}
/**
* @param cubeNumber
* the cubeNumber to set
*/
public void setCubeNumber(final String cubeNumber) {
this.cubeNumber = cubeNumber;
}
/**
* @param flightBenefit
* the flightBenefit to set
*/
public void setFlightBenefit(final FlightBenefit flightBenefit) {
this.flightBenefit = flightBenefit;
}
}
FlightBenefit.java
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
@Entity
@Table(name = "FlightBenefit")
public class FlightBenefit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private String id;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "useByTime")
private DateTime useByTime;
@Column(name = "discountAmount")
private String discountAmount;
@OneToMany(mappedBy = "flightBenefit")
private Set<FulltimeEmployee> FulltimeEmployees;
/**
* @return the discountAmount
*/
public String getDiscountAmount() {
return discountAmount;
}
/**
* @return the fulltimeEmployees
*/
public Set<FulltimeEmployee> getFulltimeEmployees() {
return FulltimeEmployees;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @return the useByTime
*/
public DateTime getUseByTime() {
return useByTime;
}
/**
* @param discountAmount
* the discountAmount to set
*/
public void setDiscountAmount(final String discountAmount) {
this.discountAmount = discountAmount;
}
/**
* @param fulltimeEmployees
* the fulltimeEmployees to set
*/
public void setFulltimeEmployees(final Set<FulltimeEmployee> fulltimeEmployees) {
FulltimeEmployees = fulltimeEmployees;
}
/**
* @param id
* the id to set
*/
public void setId(final String id) {
this.id = id;
}
/**
* @param useByTime
* the useByTime to set
*/
public void setUseByTime(final DateTime useByTime) {
this.useByTime = useByTime;
}
}
StackTrace
Jun 1, 2013 2:18:18 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jun 1, 2013 2:18:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jun 1, 2013 2:18:18 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 1, 2013 2:18:18 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 1, 2013 2:18:18 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Jun 1, 2013 2:18:18 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Jun 1, 2013 2:18:19 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 1, 2013 2:18:19 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jun 1, 2013 2:18:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Jun 1, 2013 2:18:21 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 1, 2013 2:18:21 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000115: Hibernate connection pool size: 20
Jun 1, 2013 2:18:21 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000006: Autocommit mode: false
Jun 1, 2013 2:18:21 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/TestSchema]
Jun 1, 2013 2:18:21 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000046: Connection properties: {user=user, password=****}
create table TestSchema.Employee (
employeetype varchar(31) not null,
id integer not null auto_increment,
hireDate datetime,
name varchar(255),
status varchar(255),
openAreaNumber varchar(255),
cubeNumber varchar(255),
primary key (id)
)
create table TestSchema.FlightBenefit (
id varchar(255) not null auto_increment,
discountAmount varchar(255),
useByTime datetime,
primary key (id)
)
Jun 1, 2013 2:18:23 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table TestSchema.FlightBenefit (id varchar(255)
not null auto_increment, discountAmount varchar(255), useByTime datetime, primary key
(id))
Jun 1, 2013 2:18:23 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Incorrect column specifier for column 'id'
alter table TestSchema.Employee
add index FK_opia9u461cgfe5i9vk7bo0p56 (id),
add constraint FK_opia9u461cgfe5i9vk7bo0p56
foreign key (id)
references TestSchema.FlightBenefit (id)
Jun 1, 2013 2:18:23 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table TestSchema.Employee add index
FK_opia9u461cgfe5i9vk7bo0p56 (id), add constraint FK_opia9u461cgfe5i9vk7bo0p56 foreign
key (id) references TestSchema.FlightBenefit (id)
Jun 1, 2013 2:18:23 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Cannot add foreign key constraint
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/TestSchema]
Jun 1, 2013 2:18:23 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000115: Hibernate connection pool size: 20
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000006: Autocommit mode: false
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/TestSchema]
Jun 1, 2013 2:18:23 PM
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000046: Connection properties: {user=user, password=****}
Jun 1, 2013 2:18:23 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jun 1, 2013 2:18:23 PM
org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jun 1, 2013 2:18:23 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into TestSchema.FlightBenefit (discountAmount, useByTime) values (?,
?)
Jun 1, 2013 2:18:25 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
Jun 1, 2013 2:18:25 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table 'TestSchema.flightbenefit' doesn't exist
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not
execute statement
at
org.hibernate.exception.internal.SQLExceptionTypeDelegate.
convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.
convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.
convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.
convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.
executeUpdate(ResultSetReturnImpl.java:136)
at
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.
executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.
performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.
insert(AbstractEntityPersister.java:2975)
at org.hibernate.persister.entity.AbstractEntityPersister.
insert(AbstractEntityPersister.java:3487)
at org.hibernate.action.internal.EntityIdentityInsertAction.
execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.
addResolvedEntityInsertAction(ActionQueue.java:214)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.
addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.
performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.
performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.
saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.
saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.
saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.
entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.
performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.
onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at Main.main(Main.java:34)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table
'TestSchema.flightbenefit' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.
executeUpdate(ResultSetReturnImpl.java:133)
... 22 more
回答1:
Answer to original question:
Following is incorrect, because FulltimeEmployee
does not have persistent attribute named FlightBenefit
@OneToMany(mappedBy = "FlightBenefit")
private Set<FulltimeEmployee> FulltimeEmployees;
Names of persistent attributes are case sensitive. Because attribute is flightBenefit
(first character is lower case), name should be exactly same when used in mappedBy:
@OneToMany(mappedBy = "flightBenefit")
Additionally as warnings are telling, using Table annotation in subclass does not make sense with SINGLE_TABLE: they are persisted to single table so consequently table is same.
Answer to question after modification:
Now problem comes from:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private String id;
That does not work, because there is no auto increment for String (varchar in database). That was also told in logs in following message: Incorrect column specifier for column 'id'. When feasible, use Integer/Long instead.
来源:https://stackoverflow.com/questions/16871012/hibernate-one-to-many-mapping-error-mappedby-reference-an-unknown-target-entit