如何在mySQL

时间:2016-07-26 00:12:16

标签: java mysql hibernate

我想使用Hibernate(4.3.6 Final)将基本类型的Map作为键和非实体可嵌入复合类型值保存为值。

我没有基本类型的任何问题作为关键和价值我以同样的方式完成所有事情并且没关系。当我将地图的值更改为可嵌入的复合类型时,我有问题,如下所示。

但我收到了错误:

  

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列' hash'在'字段列表'

你可以帮助我做错了什么或我错过了什么?

表格准备保留我的地图

image of table user_address

我的地图将保留在实体USER(片段)

@ElementCollection
@CollectionTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))
@MapKeyColumn(name="ADDRESS_TYPE")
@AttributeOverrides({@AttributeOverride(name="addressLine1", column=@Column(name="USER_ADDRESS_LINE_1")),
        @AttributeOverride(name="addressLine2", column=@Column(name="USER_ADDRESS_LINE_2"))})
private Map<String,Address> address = new HashMap<String, Address>();

可嵌入复合类型ADDRESS(片段)

@Embeddable
public class Address {

@Column(name="ADDRESS_LINE_1")
private String addressLine1;

@Column(name="ADDRESS_LINE_2")
private String addressLine2;

@Column(name="CITY")
private String city;

@Column(name="STATE")
private String state;

@Column(name="ZIP_CODE")
private String zipCode;

public Address() {
}

主要方法 公共类申请{

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    try {
        Transaction transaction = session.beginTransaction();


        User user = new User();

        Address address = new Address();
        Address address2 = new Address();
        setAddressFields(address);
        setAddressFields2(address2);
        user.getAddress().put("ONE",address);
        user.getAddress().put("TWO",address2);
        setUserFields(user);

        session.save(user);

        transaction.commit();

    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        session.close();
        HibernateUtil.getSessionFactory().close();
    }


}

private static void setUserFields(User user) {
    user.setAge(22);
    user.setBirthDate(new Date());
    user.setCreatedBy("kmb");
    user.setCreatedDate(new Date());
    user.setEmailAddress("kmb385");
    user.setFirstName("Kevin");
    user.setLastName("bowersox");
    user.setLastUpdatedBy("kevin");
    user.setLastUpdatedDate(new Date());
}

private static void setAddressFields(Address address) {
    address.setAddressLine1("Line 1");
    address.setAddressLine2("Line 2");
    address.setCity("New York");
    address.setState("NY");
    address.setZipCode("12345");
}

private static void setAddressFields2(Address address) {
    address.setAddressLine1("Line 3");
    address.setAddressLine2("Line 4");
    address.setCity("Corning");
    address.setState("NY");
    address.setZipCode("12345");
}

}

错误全文:

  

DEBUG - 插入USER_ADDRESS(USER_ID,哈希,值,ADDRESS_LINE_1,ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?,?)   Hibernate:插入USER_ADDRESS(USER_ID,哈希,值,ADDRESS_LINE_1,ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?,?)   DEBUG - 无法执行声明[不适用]   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列&#39; hash&#39;在&#39;字段列表&#39;       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)       在com.mysql.jdbc.Util.getInstance(Util.java:387)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)       在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)       在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)       在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)       在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)       在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)       at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)       在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)       在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)       在org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)       at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)       at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)       在org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)       在org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)       在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)       在org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)       at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)       at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)       在com.infiniteskills.data.Application.main(Application.java:33)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   警告 - SQL错误:1054,SQLState:42S22   错误 - 未知列&#39;哈希&#39;在&#39;字段列表&#39;   INFO - HHH000010:在批量发布时,它仍然包含JDBC语句   DEBUG - HHH000420:关闭未发布的批次   DEBUG - 释放JDBC连接   DEBUG - 发布的JDBC连接   调查 - HHH000031:结束   调查 - 在取消注册所有子服务委员会时隐式销毁ServiceRegistry   org.hibernate.exception.SQLGrammarException:无法执行语句       在org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)       在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)       在org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)       at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)       at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)       在org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)       在org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)       在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)       在org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)       at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)       at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)       在com.infiniteskills.data.Application.main(Application.java:33)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列&#39;哈希&#39;在&#39;字段列表&#39;       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)       在com.mysql.jdbc.Util.getInstance(Util.java:387)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)       在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)       在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)       在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)       在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)       在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)       at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)       在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)       在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)       ......还有17个   INFO - HHH000030:清理连接池[jdbc:mysql:// localhost:3306 / ifinances]   DEBUG - 在取消注册所有子ServiceRegistries时隐式销毁Boot-strap注册表

错误侦听最有趣的IMO片段

Hibernate尝试使用带有哈希值和值的sql插件,但当然我的表中没有这些列。

  

DEBUG - 插入USER_ADDRESS(USER_ID,哈希,值,ADDRESS_LINE_1,ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?,?)   Hibernate:插入USER_ADDRESS(USER_ID,哈希,值,ADDRESS_LINE_1,ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?,?)

提前感谢您的建议

1 个答案:

答案 0 :(得分:0)

我认为我已经找到了解决方案。

@AttributeOverride存在问题,我不知道为什么,因为我使用完全相同的表示法来保存地址集合(可嵌入的复合类型)

我将非常感谢帮助为什么当我在使用Map时尝试覆盖列名时出现问题,当我尝试持久保存同一嵌入复合类型的集合(地址)时一切正常

当我更改类地址以避免覆盖列名时,我的Map已成功保留。换句话说,我已经更改了Address类型的列名,以便与USER_ADDRESS中的列名完全对应。

更改了复合嵌入类型

现在列名与表中的列名完全对应,以持久映射

@Embeddable
public class Address {

    @Column(name="USER_ADDRESS_LINE_1")
    private String addressLine1;

    @Column(name="USER_ADDRESS_LINE_2")
    private String addressLine2;

    @Column(name="CITY")
    private String city;

        @Column(name="STATE")
    private String state;

    @Column(name="ZIP_CODE")
    private String zipCode;

    public Address() {
    }
...

在没有覆盖的情况下保留地图

@ElementCollection
@CollectionTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))
@MapKeyColumn(name="ADDRESS_TYPE")
@Columns(columns = {
        @Column(name="USER_ADDRESS_LINE_1"),
        @Column(name="USER_ADDRESS_LINE_2"),
        @Column(name="CITY"),
        @Column(name="STATE"),
        @Column(name="ZIP_CODE")
})
private Map<String,Address> address = new HashMap<String, Address>();

<强>结果

在保存地图后从表USER_ADDRESS中选择

select from table USER_ADDRESS after map has been saved

  

Hibernate:插入USER_ADDRESS(USER_ID,ADDRESS_TYPE,USER_ADDRESS_LINE_1,USER_ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?)   DEBUG - 插入USER_ADDRESS(USER_ID,ADDRESS_TYPE,USER_ADDRESS_LINE_1,USER_ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?)   Hibernate:插入USER_ADDRESS(USER_ID,ADDRESS_TYPE,USER_ADDRESS_LINE_1,USER_ADDRESS_LINE_2,CITY,STATE,ZIP_CODE)值(?,?,?,?,?,?,?)   DEBUG - 完成插入集合:插入2行   DEBUG - 已提交的JDBC连接   DEBUG - HHH000420:关闭未发布的批次   DEBUG - 释放JDBC连接   DEBUG - 发布的JDBC连接   调查 - HHH000031:结束   调查 - 在取消注册所有子服务委员会时隐式销毁ServiceRegistry   INFO - HHH000030:清理连接池[jdbc:mysql:// localhost:3306 / ifinances]   DEBUG - 在取消注册所有子ServiceRegistries时隐式销毁Boot-strap注册表