JPA更新查询问题

时间:2014-06-26 05:43:31

标签: java jpa

假设我们有2个实体

@Entity(name = "user")
@Inheritance(strategy = InheritanceType.JOINED)
public class User{

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 private String name;
}

另一个继承类

@Entity
public class ExtendedUser extends User{
   private String bio;
}

现在,当我尝试运行JPA更新查询时

@Modifying
@Query("update ExtendedUser  set bio=?2 where id=?1")
void updateOverflowText(Long id,String bio);

我得到一个例外,因为含糊不清的列" id"

而无法运行查询
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' in where clause is ambiguous
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
 at com.mysql.jdbc.Util.getInstance(Util.java:386)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
 at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)
 at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)
 at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)
 at org.hibernate.hql.ast.exec.MultiTableUpdateExecutor.execute(MultiTableUpdateExecutor.java:142)
 ... 103 more

2 个答案:

答案 0 :(得分:2)

试试这个

  

更新用户usr设置usr.bio =?2其中usr.id =?1

答案 1 :(得分:-1)

试试这个:

@Modifying 
@Query( nativeQuery = true, value = "update user set bio=?2 where id=?1")
int updateOverflowText( Long id, String bio );    `