注释多对一的休眠关系

时间:2019-04-29 10:07:24

标签: java mysql hibernate jpa

运行该方法获取患者地址时,休眠状态表示该表不存在

与患者的关系地址;

OneToMany(
            targetEntity = Address.class,
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private Set<Address> addresses = new HashSet<Address>();

加入地址类中的列

 @ManyToOne
    @JoinColumn (name = "Patient_Id", nullable = false)
    private Patient patient;

运行查询以获取患者然后打印出许多地址时,它说该表不存在。 查询以提取患者

 @NamedQuery(
                        name = "findPatientByFullName",
                        query = "select p from Patient p where p.firstName = :firstName AND p.lastName = :lastName"
                )
System.out.println(sM.getPatientByFullName("Tom", "Test").get(0).getFirstName());
       List<Patient> patients = sM.getPatientByFullName("Tom", "Test");

通过全名方法获取患者

public List<Patient> getPatientByFullName(String firstName, String lastName) {
        List patientsName = null;
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            TypedQuery query = session.getNamedQuery("findPatientByFullName");
            query.setParameter("firstName", firstName);
            query.setParameter("lastName", lastName);
            patientsName = query.getResultList();
        }catch (HibernateException e){
            e.printStackTrace();
        }return patientsName;
    }

获取患者列表,但是当我想查找地址列表时会出错。

Apr 29, 2019 10:58:38 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
Apr 29, 2019 10:58:38 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table 'KYMb02vi2Z.Patient_PatientEmailAddress' doesn't exist
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:419)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:191)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:691)
    at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2286)
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:585)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:263)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:581)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:148)
    at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:177)
    at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:162)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:263)
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:161)
    at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:168)
    at SessionManager.main(SessionManager.java:45)
Caused by: java.sql.SQLSyntaxErrorException: Table 'KYMb02vi2Z.Patient_PatientEmailAddress' doesn't exist
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1020)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
    ... 18 more

堆栈跟踪错误

1 个答案:

答案 0 :(得分:0)

仔细查看错误。它为您显示JSON,但找不到。所以请再次检查代码中的内容并告诉我。还要检查您的映射器,确定您通过了什么。 谢谢