在hibernate中的多对多映射中只插入一个表

时间:2016-10-04 05:38:59

标签: java mysql hibernate

我有两个表A和B.表B已经有数据。我不想将数据插入表B,但只能插入表A. 表A的一行对应于表B的许多行,表B的一行对应于表A的许多行。 我还想将数据插入其关系表A_B中。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

这是我的ParcelTracking类:

private int tracking_ID;
private Date update_DateTime;
private Set<TrackingStatus> trackingStatus;
private String comments; 
// getter,setter and constructor

TrackingStatus类:

private int trackingStatus_ID;
private String trackingStatus_Desc;
private String comments;
private ParcelTracking parcelTracking;
//Getter, setter and constructor

parceltracking.hbm.xml:

<class name = "org.trackmodel.ParcelTracking" table = "parceltracking">
  <id name = "tracking_ID" column = "Tracking_ID">
  </id>
  <property name = "update_DateTime" column = "Update_DateTime"></property>
  <property name = "comments" column = "Comments"></property>
  <set name = "trackingStatus" table = "parcel_status" cascade = "none" >
     <key column = "Tracking_ID"></key>
     <many-to-many class = "org.trackmodel.TrackingStatus" column = "TrackingStatus_ID"></many-to-many>
  </set>
</class>

trackingstatus.hbm.xml:

 <class name = "org.trackmodel.TrackingStatus" table = "trackingstatus">
    <id name = "trackingStatus_ID" column = "TrackingStatus_ID">
    </id>
    <property name = "trackingStatus_Desc" column = "TrackingStatus_Desc"></property>
    <property name = "comments" column = "Comments"></property>
    <join table = "parcel_status" inverse = "true">
      <key column = "TrackingStatus_ID"></key>
      <many-to-one name = "parcelTracking" class = "org.trackmodel.ParcelTracking" column = "Tracking_ID"/>
    </join>
 </class>

这里我使用RandomCode生成器类生成tracking_ID:

private static final int MAX = 99999999;
private static final int MIN = 11111111;

public int generateCode(){

    Random random = new Random();
    int code = random.nextInt(MAX-MIN+1)+MIN;
    return code;

}

我已经在trackingstatus表中有很多数据,TrackingStatus_ID从20开始。我不想在将数据插入parceltracking表时插入数据。一行parceltracking对应于trackingstatus表中的行数。

我正在尝试运行以下代码:

识别TestClass:

    Set<TrackingStatus> statuses = new HashSet<TrackingStatus>();

    RandomCode randomCode = new RandomCode();

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

    Session session = sessionFactory.getCurrentSession();

    session.beginTransaction();

    int trackingCode = randomCode.generateCode();

    ParcelTracking parcelTracking = new ParcelTracking();
    parcelTracking.setTracking_ID(trackingCode);
    parcelTracking.setComments("NO COMMENT PLEASE");
    parcelTracking.setUpdate_DateTime(new Date());

    TrackingStatus trackingStatus = session.get(TrackingStatus.class,22);
    session.setReadOnly(trackingStatus, true);
    statuses.add(trackingStatus);
    parcelTracking.setTrackingStatus(statuses);

    session.saveOrUpdate(parcelTracking);
    session.getTransaction().commit();
    session.close();

我收到以下错误:

 org.hibernate.exception.GenericJDBCException: could not execute statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2921)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at org.tracktest.TestClass.main(TestClass.java:44)
Caused by: java.sql.SQLException: Field 'TrackingStatus_ID' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 18 more

如何在不插入trackingstatus表的情况下将数据插入parceltracking表? 两个表之间的关系必须是一对多。