1

I have interesting situation. I have parsed several news posting websites and want to save them to database through scheduler. But the error occures while saving. Because of transactional write-behind condition described here.

My model class is

@Entity
@Table(name="News")
public class News {

    @Id
    @GeneratedValue(strategy = javax.persistence.GenerationType.TABLE)
    private Long id;

    @Column(name="title")
    private String title;

    @Column(name="entity")
    private String entity;

    @Column(name="text")
    private String newsText;

    @Column(name="url")
    private String url;

    @Column(name="newsDate")
    private Date newsDate;
    //getters and setters}

my hibernate settings (Spring boot ones)

    spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/projectAn
spring.datasource.username=postgres
spring.datasource.password=Iskandar123

My spring Batch scheduled method

   @Scheduled(initialDelay=5000,fixedRate=5000)
    private void mferNews()
    {
        Document doc;
        try
        {
             doc = Jsoup.connect("http://www.mfer.uz/ru/news/news-uzb/").get();

            Elements links = doc.select("a[href^=/ru/news/news-uzb/");

            for(Element link : links) {
                //System.out.println("");
               if(isMatch(link.attr("href")))
               {
                News news = new News();
                //System.out.println("http://www.mfer.uz" + link.attr("href"));
                String url = "http://www.mfer.uz" + link.attr("href");
                doc = Jsoup.connect("http://www.mfer.uz" + link.attr("href")).get();

                if(!doc.title().toString().equals("МВЭСИТ - Новости Узбекистана"))
                {
                    news.setUrl(url);
                    news.setTitle(doc.title());
                    news.setEntity("МВЭСИТ");
                    news.setNewsText("");
                    //news.setNewsDate(new Date());
                //System.out.println("Title: " + doc.title());
                Elements paragraphs = doc.select("div.detail-text").first().select("div ");
                int i = 0;
                    for(Element p :paragraphs)
                    {
                        i += 1;
                        if(i != 1)
                        {
                            news.setNewsText(news.getNewsText() +"<br />\n" + p.text());
                            //System.out.println(p.text());
                        }
                    }
                    parsingService.addNews(news);
                }

               }

            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

my ERROR LOGS

    2016-08-03 22:22:53.147  WARN 4620 --- [pool-2-thread-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2016-08-03 22:22:53.147 ERROR 4620 --- [pool-2-thread-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "hibernate_sequences" does not exist
  Позиция: 36
2016-08-03 22:22:53.162 ERROR 4620 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

org.springframework.dao.InvalidDataAccessResourceUsageException: error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: error performing isolated work
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:242) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at com.sun.proxy.$Proxy75.save(Unknown Source) ~[na:na]

I am stack in that problem almost last 2 days.

i am stack in this problem almost 2 days. i suppose that the problem is around @GeneratedValue and the strategy.

Community
  • 1
  • 1
ISkandar
  • 17
  • 2
  • 9
  • Try changing data type `Long` to `long`. You don't need it to be `Long` – ryekayo Aug 03 '16 at 17:30
  • is it a reason or change for good code practice? – ISkandar Aug 03 '16 at 18:03
  • Well Long allows null values.. Since you are using Spring/Hibernate and im going on a guess that your ID is your PK, you should NOT have any null values.. I just think itd be better practice to change that it to `long` – ryekayo Aug 03 '16 at 18:07

2 Answers2

2

Try to replace strategy on IDENTITY, like this:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
Serge Nikitin
  • 1,628
  • 5
  • 19
  • 27
1

I also faced the same issue. But quite easily fixed it by just creating a sequence called "hibernate_sequence"in the Database. You will find the sequence above tables in the Database.

shubham
  • 611
  • 7
  • 16