两个问题 - 如果我查询一个实体,我会得到......
ERROR: ORA-00933: SQL command not properly ended
Nov 11, 2013 8:47:36 PM org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
如果我尝试插入一个实体,我会得到......
Hibernate: insert into MyDB.dbo.BOSS_CONTACTS_C_LVL (CNTCT_PHN_NBR, CVM_CNTCT_EMAIL, DTID, BUSINESS_NAME, CONTACT_NAME, FIRST_NAME, LAST_NAME, PHONE_NUMBER, CONTACT_TITLE, EMPLOYEES_HERE, REGION, AREA, BRANCH_NAME, BRANCH_ID, IN_OUTBOUND, PROSPECT_TYPE, RATING, DUNS_NUMBER, ADDRESS, CITY, STATE, ZIP, LOCAL_STREET, LOCAL_CITY, LOCAL_STATE_PROVINCE, LOCAL_ZIP_POSTAL_CODE, HANDSET_POTENTIAL, CAMPAIGN_NAME, CAMPAIGN_CODE, ASSIGNED_REP, TOTAL_TELECOM, PROFILE_ID, BUSINESS_ID, KEYCODE, SIC, CVM_UNQ_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Nov 11, 2013 9:00:53 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 926, SQLState: 42000
Nov 11, 2013 9:00:53 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00926: missing VALUES keyword
我尝试了几种不同的 Oracle 驱动程序,认为这可能是问题所在。我还在某处阅读了一篇文章,其中一个字段名称可能是 Oracle 关键字,但我删除了除要测试的主键之外的所有字段,但错误仍然存在。
休眠.cfg -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">oasisd_02</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@MyServer:1521/MyDB</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.default_catalog">MyDB</property>
<property name="hibernate.default_schema">dbo</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<mapping class="myPrj.domain.Contact" resource="myPrj/domain/Contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射文件是从 Eclipse 中的 JBOSS 工具生成的。
另外,我尝试过 JDK 1.6 和 1.7
更新- 我应该补充一点,我没有使用自定义 HQL 查询。
使用存储库模式 -
public class ContactRepository implements IContactRepository {
public Contact find(String cvmUnqId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Contact aContact = (Contact) session.load(Contact.class, cvmUnqId);
return aContact;
}
public Contact findByZipCode(String zipcode) {
// TODO Auto-generated method stub
return null;
}
public void store(Contact contact) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(contact);
session.getTransaction().commit();
}
public void remove(Contact contact)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(contact);
session.getTransaction().commit();
}
}
和测试用例 -
@Test
public void testContactList(){
ContactRepository contactRepo = new ContactRepository();
Object result = contactRepo.find("FakeID");
assertEquals(result, null);
}
@Test
public void addRemoveContactTest() {
ContactRepository contactRepo = new ContactRepository();
Contact contact = new Contact();
contact.setAddress("123 Anywhere");
contact.setArea('A');
contact.setAssignedRep('B');
contact.setBranchId('C');
contact.setBranchName('D');
contact.setBusinessId("Business ID");
contact.setBusinessName("Fake Business");
contact.setZip("11111");
contact.setCvmUnqId("FakeID");
contactRepo.store(contact);
Contact foundContact = contactRepo.find(contact.getCvmUnqId());
assertEquals("Contact was not found in repository", contact, foundContact);
contactRepo.remove(contact);
foundContact = contactRepo.find(contact.getCvmUnqId());
assertEquals("Contact was not removed from repository", null, foundContact);
}
以及工厂实用程序以防万一 -
public class HibernateUtil {
private static SessionFactory sessionFactory = configureSessionFactory();
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
问题解决了。多部分表标识符不正确。我从休眠配置中删除了默认目录属性,现在它工作正常。我很惊讶这不是来自 Oracle 的更好的错误消息。诸如“表不存在”之类的东西……</p>