AnnotationException:org.am.Car.engine上的@OneToOne或@ManyToOne引用了一个未知实体:org.am.Engine

时间:2016-10-21 06:16:04

标签: java hibernate

1.Car.java

@Entity
@Table(name="CAR_ONE2ONE_ANN")
public class Car {
@Id
@Column(name="CAR_ID")
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
@Column(name="NAME")
private String name = null;
@Column(name="COLOR")
private String color = null;    
@OneToOne (cascade= CascadeType.ALL)
@JoinColumn(name="ENGINE_ID")
private Engine engine = null;

2.Engine.java

@Entity
@Table(name="ENGINE_ONE2ONE_ANN")
public class Engine {    
@Id
@Column(name="ENGINE_ID")
@GeneratedValue(strategy= GenerationType.AUTO)
private int id = 0;
@Column(name="MAKE")
private String make = null;
@Column(name="MODEL")
private String model = null;
@Column(name="SIZE")
private String size = null;    
@OneToOne(mappedBy="car")
private Car car = null;

3.hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/JH</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">amar</property>    
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>     
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>        
<mapping class="org.amar.associations.one2one.ann.Car"></mapping>
<mapping class="org.amar.associations.one2one.ann.Engine"></mapping>
</session-factory>

4.OneToOneTest.java

public class OneToOneTest {  
private SessionFactory factoryV2 = null;
private void initV2() {
    Configuration config =
    new Configuration().configure("associations/one2one/v2/hibernate.cfg.xml");
    ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();      
    factoryV2 = config.buildSessionFactory(registry);      
} 
private void persistV2() {
    Session session = factoryV2.getCurrentSession();
    session.beginTransaction();        
    List<Car> cars = createCars(10);
    for (Car car : cars) {
        session.save(car);
    }
    session.getTransaction().commit();
    System.out.println("V2 Done");
} 
private void v2() {
    initV2();       
    persistV2();
}
private List<Car> createCars(int numberOfCars) {
    Car car = null;
    Engine engine = null;
    List<Car> cars = new ArrayList<Car>();
    for (int i = 1; i < numberOfCars; i++) {
        engine = new Engine();
        engine.setId(100+i);
        engine.setMake("V8 Series " + i);
        engine.setModel("DTS -" + i + 5);
        engine.setSize("1.6 V8 GAS" + i + 2);
        car = new Car();
        car.setId(i);
        car.setName("Cadillac ATS Sedan " + i);
        car.setColor("White");
        car.setEngine(engine);
        cars.add(car);
    }
    return cars;
}
public static void main(String[] args) {
    OneToOneTest p = new OneToOneTest();     
    p.v2();
}
}

代码给出了异常 线程&#34; main&#34;中的例外情况org.hibernate.AnnotationException:org.am.Car.engine上的@OneToOne或@ManyToOne引用了一个未知实体:org.am.Engine

1 个答案:

答案 0 :(得分:0)

@OneToOne(mappedBy="car")
private Car car = null;

应该是:

@OneToOne(mappedBy="engine")
private Car car = null;