Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MappingException:外键:category[category_ID])必须';vesame作为引用主键的列数(类别[股票ID,类别ID])_Java_Hibernate - Fatal编程技术网

Java MappingException:外键:category[category_ID])必须';vesame作为引用主键的列数(类别[股票ID,类别ID])

Java MappingException:外键:category[category_ID])必须';vesame作为引用主键的列数(类别[股票ID,类别ID]),java,hibernate,Java,Hibernate,我真的在解决下面的错误请帮我解决这个问题 我正在开发许多关系示例 Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK302BCFEEB260666:category [CATEGORY_ID])) must have same number of columns as the referenced primary key (category [STOCK_ID,CATEGOR

我真的在解决下面的错误请帮我解决这个问题

我正在开发许多关系示例

Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK302BCFEEB260666:category [CATEGORY_ID])) must have same number of columns as the referenced primary key (category [STOCK_ID,CATEGORY_ID])
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.mkyong.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at com.mkyong.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.mkyong.App.main(App.java:13)
Caused by: org.hibernate.MappingException: Foreign key (FK302BCFEEB260666:category [CATEGORY_ID])) must have same number of columns as the referenced primary key (category [STOCK_ID,CATEGORY_ID])
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:112)
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:95)
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1808)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1729)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1396)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1829)
    at com.mkyong.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more
App.java

public class App {
    public static void main(String[] args) {
        System.out.println("Hibernate many to many (Annotation)");
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Stock stock = new Stock();
        stock.setStockCode("7052");
        stock.setStockName("PADINI");

        Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
        Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY");

        Set<Category> categories = new HashSet<Category>();
        categories.add(category1);
        categories.add(category2);

        stock.setCategories(categories);

        session.save(stock);

        session.getTransaction().commit();
        System.out.println("Done");
    }
}
编辑-1: 现在我的错误率已降至最低?

Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.mkyong.App.main(App.java:31)
Caused by: java.sql.BatchUpdateException: Table 'test.stock_category' doesn't exist
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more
联接表的名称假定为 连接在一起的关联主表(拥有方优先) 使用下划线

在这种情况下,联接表是stock\u类别

@JoinTable(name = "stock_category", 
        joinColumns = { @JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID")})
public class App {
    public static void main(String[] args) {
        System.out.println("Hibernate many to many (Annotation)");
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Stock stock = new Stock();
        stock.setStockCode("7052");
        stock.setStockName("PADINI");

        Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
        Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY");

        Set<Category> categories = new HashSet<Category>();
        categories.add(category1);
        categories.add(category2);

        stock.setCategories(categories);

        session.save(stock);

        session.getTransaction().commit();
        System.out.println("Done");
    }
}
create table stock(
   stock_id BIGINT(10) NOT NULL AUTO_INCREMENT,
   stock_code VARCHAR(50),
   stock_name VARCHAR(50),
   PRIMARY KEY (`stock_id`)
);


create table Category(
   stock_id BIGINT(10) NOT NULL AUTO_INCREMENT,
   category_id VARCHAR(50),
   name VARCHAR(50),
   description VARCHAR(50),
   PRIMARY KEY (`stock_id`)
)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.mkyong.App.main(App.java:31)
Caused by: java.sql.BatchUpdateException: Table 'test.stock_category' doesn't exist
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more
@JoinTable(name = "stock_category", 
        joinColumns = { @JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID")})