database - Hibernate一对多映射错误-mappedBy reference an unknown target entity property错误

标签 database hibernate mapping one-to-many schemaexport

我正在尝试通过从域对象创建表来测试一对多映射,但我看到错误ma​​ppedBy 引用未知的目标实体属性。有人可以看一下吗?

谢谢

员工.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;
}

}

员工状态

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();

}
}

错误堆栈跟踪

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)

感谢您的回复。我做了您提到的更改,还做了一些其他更改,例如将 session.save(flightBenefit) 添加到 Main.java 和 insertable = false, updatable = false with @JoinColumn注解。我看到正在创建 Employee 表但没有创建 FlightBenefit 并且看到了错误

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'TestSchema.flightbenefit'不存在

以下是具有更新更改和堆栈跟踪的类

主.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;
}

}

堆栈跟踪

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

最佳答案

原始问题的答案:

以下是不正确的,因为 FulltimeEmployee 没有名为 FlightBenefit 的持久属性

@OneToMany(mappedBy = "FlightBenefit")
private Set<FulltimeEmployee> FulltimeEmployees;

持久属性的名称区分大小写。因为属性是flightBenefit(第一个字符是小写),所以在mappedBy中使用时名称应该完全相同:

@OneToMany(mappedBy = "flightBenefit")

此外,如警告所述,在子类中使用表注释对 SINGLE_TABLE 没有意义:它们被持久化到单个表,因此表是相同的。

修改后的问题答案:

现在问题来自:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private String id;

那行不通,因为字符串(数据库中的 varchar)没有自动递增。日志中的以下消息也说明了这一点:列“id”的列说明符不正确。在可行的情况下,改用 Integer/Long。

关于database - Hibernate一对多映射错误-mappedBy reference an unknown target entity property错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16871012/

相关文章:

c# - 如何以没有前导零的 yyddd 格式格式化日期

sql-server - OLTP 中的索引 View ?

java - 在收到对等方的 close_notify 之前关闭入站

c# - 我可以使用自动映射器将多个对象映射到目标对象吗

javascript - D3 投影未设置 cx 属性

python - 记录高频数据而不丢包

Java应用程序不断检查表中添加的新行

java - 我如何从使用 Hibernate Jpa 的多对多关系创建的关联表中获取结果

java - Hibernate - 删除对象

php - 使用另一个数组的值按键过滤数组,并按第二个数组对结果排序