0

The DB I want to use is something like:

User Table: User_ID ...

User_Action Table: User_ID (fk to User.User_ID, should be one-to-one mapping) ...

So I have this class:

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

private long _ID;
@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "User_ID", nullable = false, unique = true)
    public long get_ID() {
        return _ID;
    }

    public void set_ID(long user_ID) {
        _ID = user_ID;
    }
...
}

and in another class, I have:

@Entity
@Table(name="User_Action")
public class UserAction implements IEntity {

    private User _user;
    @Id
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "User_ID")
    public User get_user() {
        return _user;
    }
    public void set_user(User _user) {
        this._user = _user;
    }
...
}

However running the code throws the following error in initialization time:

org.hibernate.MappingException: Could not determine type for: mycustom.User, for columns: [org.hibernate.mapping.Column(_user)]
org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
org.hibernate.mapping.RootClass.validate(RootClass.java:193)
org.hibernate.cfg.Configuration.validate(Configuration.java:1099)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1284)
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)

...

The hibernate versions I am using are:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>

I was suspecting something is wrong with the annoticationConfiguration thing, but not able to confirm it or further debug.. or is it something else?

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • 1
    You annotated your User property with Id annotation. But that annotation can only be applied to certain types (see here for details: http://docs.oracle.com/javaee/6/api/javax/persistence/Id.html). If you want to use an entity as another entity's PK, take a look at this SO post: http://stackoverflow.com/questions/904634/using-an-entity-and-their-primary-key-as-another-entitys-id. – Bohuslav Burghardt Nov 04 '14 at 21:03

1 Answers1

0

A few things to change:

1) Eliminate the underscores ("_") from the names of your classes' properties. Try to follow the Java Beans naming conventions. This is important because Hibernate expects this type of naming in order to call the classes' public accessors and mutators (aka. the getters & setters).

2) UserAction needs its own identifier property. Said property should have its getter annotated with @Id. You currently have annotated UserAction's _user property with @Id, which will undoubtedly cause problems. UserAction will have its own Primary Key, and will have User's id as a Foreign Key.

3) Consider changing the One-to-One relationship to a One-to-Many. Shouldn't a User be allowed to perform more than one UserAction?

Richard Lewan
  • 222
  • 3
  • 7
  • Thanks for your reply. Just to follow up on it: 2) UserAction is using the foreign key as its primary key, I am however not sure how to represent this in the class so I left @id for the _user in UserAction. So the question comes to, how to do the supertyping thing in hibernate? and 3) yeah it is indeed one-to-many, I just use 1-1 to keep it simple, as this is relevant to the problem. – jamesdeath123 Nov 05 '14 at 15:19