JPA/Hibernate - shared primary key

I wanted to create bidirectional one to one relationship with shared primary key.

As it is stated here JPA Hibernate One-to-One relationship I have:

public class UserProfileInformation {

    @GeneratedValue(generator = "customForeignGenerator")
        name = "customForeignGenerator",
        strategy = "foreign",
        parameters = @Parameter(name = "property", value = "userEntity")
    long id;

    private long itemsPerPage;

    private UserEntity userEntity;

@Table(name = "UserTable")
public class UserEntity {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String publicName;

    private String password;

    private String emailAddress;

    private String name; 

    private boolean active;

    private UserProfileInformation userProfileInformation;

now, when I try to persist my user in the database, I am getting null id generated for:class pl.meble.taboret.model.UserProfileInformation. Is it because, when userProfileInformation is persisted to the database userEntity doesn't have id generated at that point?

Also, what can I do to create bidirectional relationship with shared primary key in my example?

EDIT: Requested code, this is simple controller to test the operation of persisting UserEntity.

public class TestController {
    UserDao userDao;

    @RequestMapping(method= RequestMethod.GET)
    public String t(Model model){
        UserEntity entity=new UserEntity();
        UserProfileInformation p = new UserProfileInformation(entity);
        return "login";


I think the problem is with the id generation strategy. For hibernate @GeneratedValue(strategy = GenerationType.AUTO) translates into a native identifier generation. This means that hibernate expects an identity id field for the UserTable.

I don't know exactly how SQLite works in terms of identity columns, but it seems from this SO question is a little different (see the second answer).

Anyway if you plan to run your application on multiple databases is better for portability to change the id generation strategy from GenerationType.AUTO and use hibernate enhanced generators: SequenceStyleGenerator or TableGenerator. See this link in the hibernate documentation.


I tried to reproduce your problem, and it seems that SQLite dialect is not among the officially supported hibernate dialects. Meanwhile I tested your case with the H2 embeded database and it works as expected: your mappings are correct.

If you are using an unofficial SQLite dialect it might be a bug with this dialect.

Need Your Help

PHP isset not working with empty

php javascript

I'm building a form so users can add their own products to sell on a website. The javascript should check if all fields are filled out, though it isn't working. Why isn't the javascript running?

Update an MS Access database in a loop with paramaters with ms-access parameters

I was trying to update a field called 'instock'( type is double in database), where the itemcode is a value (type is long integer in access database). But when I try to update them, I get a error w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.