Hibernate SQLServerException

Odpowiedz Nowy wątek
2017-04-03 16:30

Rejestracja: 8 lat temu

Ostatnio: 2 minuty temu

0
importy

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private long id;
    private String firstName;
    private String lastName;
    private double salary;

gettery, settery
}
public class Main {

    public static void main(String[] args) {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDatabase");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Employee employee = new Employee(); 

        employee.setFirstName("Jan");
        employee.setLastName("Nowak");
        employee.setSalary(3333.3);

        entityManager.getTransaction().begin();
        entityManager.persist(employee);
        entityManager.getTransaction().commit();

        entityManager.close();
        entityManagerFactory.close();
    }

}
ERROR: could not read a hi value
com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: FOR UPDATE clause allowed only for DECLARE CURSOR.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:317)
    at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
    at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:138)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
    at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:57)
    at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:412)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:762)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:742)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747)
    at info.mike.jpa.Main.main(Main.java:31)

kwi 03, 2017 4:23:41 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1003, SQLState: S0002
kwi 03, 2017 4:23:41 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Line 1: FOR UPDATE clause allowed only for DECLARE CURSOR.
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: error performing isolated work
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:769)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:742)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747)
    at info.mike.jpa.Main.main(Main.java:31)
Caused by: org.hibernate.exception.SQLGrammarException: error performing isolated work
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    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.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:79)
    at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:412)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:762)
    ... 3 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: FOR UPDATE clause allowed only for DECLARE CURSOR.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:317)
    at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
    at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:138)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
    at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:57)
    ... 12 more

Error: com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: FOR UPDATE clause allowed only for DECLARE CURSOR.

Baza to SQL Server 2012, Na S/O i Google nic nie znalazłem. Tzn Microsoft pisze, żeby jakiegoś fixa pobrać. Pobrałem. Nic. Proszę o pomoc.

Jak potrzeba to jeszcze persistence.xml wstawię.

edytowany 1x, ostatnio: slayer9, 2017-04-03 16:31

Pozostało 580 znaków

2017-04-03 17:03

Rejestracja: 8 lat temu

Ostatnio: 2 minuty temu

Jakby ktoś miał podobny problem to SQLServer chyba nie toleruje komendy FOR UPDATE i trzeba pamiętać o ustawieniu dialektu SQLServer2012Dialect w persistence.xml oraz @GeneratedValue(strategy = GenerationType.IDENTITY).

A tak z ciekawości czemu korzystasz akurat z tej bazy danych? - scibi92 2017-04-03 20:40
Jak wybierałem bazę danych to nie miałem o nich za bardzo pojęcia, ale książka do SQLa, którą akurat miałem pod ręką zasugerowała SQL Server, bo niby funkcjonalna, darmowa, zgodna z ANSI SQL3 itd. Ale jak teraz przeglądam materiały nt Hibernate w internecie to mam wrażenie, że większość jest pod MySQL i na początku była potężna dawka frustracji dlaczego nic mi nie działa ; d - slayer9 2017-04-03 20:58
Ja polecam PostgreSQL - scibi92 2017-04-03 21:00

Pozostało 580 znaków

Odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0