-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Combing MySQL IDENTITY with Hibernate assigned generator
PostPosted: Fri Nov 24, 2017 2:53 am 
Newbie

Joined: Fri Nov 24, 2017 2:47 am
Posts: 1
I have an application which was running on H2 database and Hibernate is the ORM tool. Currently, I am changing this application to use mysql database instead of H2 database and while doing this I came to this issue when saving flagjp entity.

Here is the FlagJP entity that caused this issue.

Code:
@Entity
public class FlagJP extends BaseModelJP {

    @Id
    @GeneratedValue(generator = "IdOrGenerated")
    @GenericGenerator(name = "IdOrGenerated", strategy = "com.jp.menu.api.model.JPSequenceGenerator")
    private Long flagId;

    private String flagKey;

    @OneToMany(mappedBy="flag", cascade = CascadeType.ALL)
    private List<FlagLangJP> flagLangs = new ArrayList<>();

    @ManyToOne
    private FlagCategoryJP flagCategory;


Here are the related entities for the FlagJP

Second entity

Code:
@Entity
public class FlagLangJP extends BaseModelJP {

    @Id
    @GeneratedValue(generator = "IdOrGenerated")
    @GenericGenerator(name = "IdOrGenerated", strategy = "com.jp.menu.api.model.JPSequenceGenerator")
    private Long id;

    private String languageCode;

    private String flagName;

    private String flagDescription;

    @ManyToOne
    private FlagJP flag;



Third Entity

Code:
@Entity
public class FlagCategoryJP extends BaseModelJP {

    @Id
    @GeneratedValue(generator = "IdOrGenerated")
    @GenericGenerator(name = "IdOrGenerated", strategy = "com.jp.menu.api.model.JPSequenceGenerator")
    private Long flagCategoryId;

    private String flagCategoryName;

    @OneToMany(mappedBy = "flagCategory")
    private List<FlagJP> flags;


While looking into this issue, I was able to figure out that this is cased by FlagJP table schema not having auto increment set in the database when hibernate generated the DDL.

here is the DDL of FlagJP

Image

If I try to manually set the auto increment by executing a sql query, then mysql throw this error.

Code:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1833: Cannot change column 'flagId': used in a foreign key constraint 'FK_sk95esyf1n0gt1qqmlmdmq0uw' of table 'butterfly_emenu.flaglangbpa'
SQL Statement:


my question is , this problem does not happen when using H2 database. how to solve this issue using hibernate when the database is mysql.

Thanks in advance for any advice

Update:

Here is the code for sequence generator I am using

Code:
public class JPSequenceGenerator extends IdentityGenerator {

    private static final Logger logger = LoggerFactory.getLogger(JPSequenceGenerator.class);

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);

        if (id == null) {
            id = super.generate(session, object);

        }



        return id;
    }

}


Top
 Profile  
 
 Post subject: Re: java.sql.SQLException: Field 'id' doesn't have a default val
PostPosted: Fri Nov 24, 2017 6:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I hope you don't rely on the hbm2ddl tool in production because we don't advise you to do so.

Hbm2ddl is good for testing Hibernate or for generating an initial script, but afterward, you should be using an automatic schema migration tool, like Flyway, and store the DDL migration script along with your code.

Hibernate will only generate the IDENTITY column based on what AUTO resolved too. For Hibernate 5 and MySQL it resolves to TABLE instead of IDENTITY, as explained in this article.

Now, I wrote [url=https://vladmihalcea.com/how-to-combine-the-hibernate-assigned-generator-with-a-sequence-or-an-identity-column/an article that does something similar as you do[/url] and my mapping looks as follows:

Code:
@Id
@GenericGenerator(
   name = "assigned-identity",
   strategy = "com.jp.menu.api.model.JPSequenceGenerator"
)
@GeneratedValue(
   generator = "assigned-identity",
   strategy = GenerationType.IDENTITY
)
private Long id;


I've just tried it on MySQL and it works like a charm. You can fork my GitHub repo to see how it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.