hibernate 复合主键

JYToscer 发布于 2012/07/06 15:20
阅读 1K+
收藏 0

上图是数据库的关系图

上图是出问题表的外键,但是外键都是正确的,不知道为什么hibernate执行插入的时候会报错

1.问题出在获得PRprovider_has_PRgoods实体以后,同过getPrfiletype()去得他有的Prfiletype集合是空的。但是打印出来的查询sql我拿到数据库去查是有查询结果的。

这是生成的sql:

Hibernate: 

    select

        prfiletype0_.PRprovider_has_PRgoods_PRprovider_idPRprovider as PRprovider1_13_1_,

        prfiletype0_.PRprovider_has_PRgoods_PRgoods_idPRgoods as PRprovider2_13_1_,

        prfiletype0_.PRfileType_idPRfileType as PRfileType3_1_,

        prfiletype1_.idPRfileType as idPRfile1_4_0_,

        prfiletype1_.info as info4_0_,

        prfiletype1_.name as name4_0_,

        prfiletype1_.scope as scope4_0_ 

    from

        mydb.prprovider_has_prgoods_has_prfiletype prfiletype0_ 

    inner join

        mydb.prfiletype prfiletype1_ 

            on prfiletype0_.PRfileType_idPRfileType=prfiletype1_.idPRfileType 

    where

        prfiletype0_.PRprovider_has_PRgoods_PRprovider_idPRprovider=? 

        and prfiletype0_.PRprovider_has_PRgoods_PRgoods_idPRgoods=?

 但结果是空的。很郁闷。

2.我还尝试了先给这个空集合添加新的Prfiletype实体,然后再关联到PRprovider_has_PRgoods的实体上,结果就是报 PRprovider_has_PRgoods_has_PRfileType表与PRprovider_has_PRgoods表外键设置错误(这里是一个复合主键的关联,但我看了没错啊),但我查看过外键,都是设置正确的。

PrproviderHasPrgoodsId prProviderHasPrgoodsId = new PrproviderHasPrgoodsId(idPrprovider, idPrgoods);
		PrproviderHasPrgoods providerhasgoods = prProviderHasPrgoodsHome.findById(prProviderHasPrgoodsId);
		
		
		req.setAttribute("providergoods", providerhasgoods);
		System.out.println("~~~~~~~~~~");
		Set<Prfiletype> filetypes = providerhasgoods.getPrfiletypes();
		
		System.out.println(filetypes.isEmpty());
		System.out.println("~~~~~~~~~~");
		req.setAttribute("fileType", providerhasgoods.getPrfiletypes());
		
		
		
		Prfiletype p = new Prfiletype("XXXXX", "CCCC");
		prFiletypeHome.attachDirty(p);
		providerhasgoods.getPrfiletypes().add(p);
		prProviderHasPrgoodsHome.attachDirty(providerhasgoods);

打印出这个sql报下面的错误了

Hibernate: 
    insert 
    into
        mydb.prprovider_has_prgoods_has_prfiletype
        (PRprovider_has_PRgoods_PRprovider_idPRprovider, PRprovider_has_PRgoods_PRgoods_idPRgoods, PRfileType_idPRfileType) 
    values
        (?, ?, ?)

结果报错如下:

严重: Servlet.service() for servlet [spring] in context with path [/p-server] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`prprovider_has_prgoods_has_prfiletype`, CONSTRAINT `pg` FOREIGN KEY (`PRprovider_has_PRgoods_PRprovider_idPRprovider`, `PRprovider_has_PRgoods_PRgoods_idPRgoods`) REFERENCES `prprovide); SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`prprovider_has_prgoods_has_prfiletype`, CONSTRAINT `pg` FOREIGN KEY (`PRprovider_has_PRgoods_PRprovider_idPRprovider`, `PRprovider_has_PRgoods_PRgoods_idPRgoods`) REFERENCES `prprovide)] with root cause
java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`prprovider_has_prgoods_has_prfiletype`, CONSTRAINT `pg` FOREIGN KEY (`PRprovider_has_PRgoods_PRprovider_idPRprovider`, `PRprovider_has_PRgoods_PRgoods_idPRgoods`) REFERENCES `prprovide)
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
	at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这里显示的外键关联还没有显示完整,不知道是不是因为这个原因。

 

大家看看怎么解决啊,慌死了。

 

加载中
0
JYToscer
JYToscer
把表关系结构改了,绕过了这个问题。
返回顶部
顶部