1

Suppose we have 2 entities

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

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

 private String name;
}

Another Inherited Class

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

Now when i try to run a JPA Update query

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

I get an exception, cannot run the query, due to ambiguous column "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
Yashpal Singla
  • 1,924
  • 4
  • 21
  • 38

2 Answers2

3

try this

update User usr set usr.bio=?2 where usr.id=?1

Rana
  • 324
  • 2
  • 8
-1

Try this:

@Modifying 
@Query( nativeQuery = true, value = "update user set bio=?2 where id=?1")
int updateOverflowText( Long id, String bio );    `
Alex Pan
  • 4,341
  • 8
  • 34
  • 45
  • 3
    I don't know, somehow answers beginning with "try" give me the feeling that the author is not sure about the effect the proposed change will have. Anyway, I would feel more confident if you added an explanation why your solution will fix the problem. - Having said that, welcome to stack overflow and thanks for sharing your knowledge :) – Bö macht Blau Nov 09 '15 at 15:28
  • Mind to [explain your solution](http://stackoverflow.com/help/how-to-answer) a bit? – Markus W Mahlberg Nov 09 '15 at 15:32