I
I
im_berzki2011-06-19 12:13:36
Java
im_berzki, 2011-06-19 12:13:36

Hibernate ORM: Mapping issue

Good day!

For several weeks, I have been unsuccessfully trying to solve the problem with hibernate ((I

re-read a bunch of documentation, googled everything I could ...

Tell me, please, what is the mistake? What am I doing wrong?



DB schema:
image


Mapping:
(to generate getters/setters I use the lombok library)

User.java

Copy Source | Copy HTML
  1. @Entity
  2. @Table(name = "users")
  3. public @Data class User
  4. {
  5.  
  6.     // Base Mapping
  7.  
  8.     @Id
  9.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  10.     @Column(name = "user_id")
  11.     @Getter @Setter Long user_id;
  12.  
  13.     @Column(name = "user_name")
  14.     private @Getter @Setter String user_name;
  15.  
  16.     @Column(name = "login")
  17.     private @Getter @Setter String login;
  18.  
  19.     @Column(name = "password")
  20.     private @Getter @Setter String password;
  21.  
  22.     // Releations Mapping
  23.  
  24.     // Releations With Project
  25.  
  26.     @ManyToMany(cascade={CascadeType.ALL})
  27.     @JoinTable(name="users_on_projects",
  28.             [email protected](name="user_id"),
  29.             [email protected](name="project_id") )
  30.             private @Getter @Setter List projects = new Vector();
     
     
        @OneToMany
        @JoinTable(name="users_on_projects",
                [email protected](name="user_id"),
                [email protected](name="users_on_projects_id") )
                private @Getter @Setter List intervals = new Vector();
    }


Project.java
Copy Source | Copy HTML
  1. @Entity
  2. @Table(name = "projects")
  3. public @Data class Project
  4. {
  5.  
  6.     // Base Mapping
  7.  
  8.     @Id
  9.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  10.     @Column(name ="project_id")
  11.     private @Getter @Setter Long project_id;
  12.  
  13.     @Column(name ="project_name")
  14.     private @Getter @Setter String project_name;
  15.  
  16.  
  17.     // Releations Mapping
  18.  
  19.     // Releations With User
  20.  
  21.       @ManyToMany(cascade={CascadeType.ALL})
  22.     @JoinTable(name="users_on_projects",
  23.             [email protected](name="project_id"),
  24.             [email protected](name="user_id") )
  25.       private @Getter @Setter List users = new Vector();
     
         @OneToMany
         @JoinTable(name="users_on_projects",
                [email protected](name="project_id"),
                [email protected](name="users_on_projects_id") )
                private @Getter @Setter List intervals = new Vector();


Interval.java
Copy Source | Copy HTML
  1. @Entity
  2. @Table(name = "intervals")
  3. public class Interval
  4. {
  5.     @Id
  6.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  7.     @Column(name = "interval_id")
  8.     @Getter @Setter Long interval_id;
  9.  
  10.     @Column(name ="start_time")
  11.     private @Setter @Getter
  12.                 @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  13.                 Date start_time;
  14.  
  15.     @Column(name ="finish_time")
  16.     private @Setter @Getter
  17.             @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  18.             Date FinishTime;
  19.  
  20.  
  21.     @ManyToOne(cascade={CascadeType.ALL})
  22.     @JoinTable(name="users_on_projects",
  23.             [email protected](name="users_on_projects_id"),
  24.             [email protected](name="user_id") )
  25.             private @Getter @Setter User user;
  26.  
  27.  
  28.     @JoinTable(name="users_on_projects",
  29.             [email protected](name="users_on_projects_id"),
  30.             [email protected](name="project_id") )
  31.             private @Getter @Setter
  32.     @ManyToOne
  33.              Project project;
  34.  
  35.  
  36.     @ManyToOne(cascade = CascadeType.ALL)
  37.     @JoinColumn(name="interval_type_id")
  38.     private @Getter @Setter IntervalType intervalType;
  39.  
  40. }


IntervalType.java
Copy Source | Copy HTML
  1. @Entity
  2. @Table(name = "interval_type")
  3. public class IntervalType
  4. {
  5.     @Id
  6.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  7.     @Column(name = "interval_type_id")
  8.     @Getter @Setter Long interval_type_id;
  9.  
  10.     @Column(name = "interval_type_name")
  11.     private @Getter @Setter String interval_type_name;
  12. }


Заранее благодарен!

UPD: На чтение все заработало.
Но, осталась проблема с записью интервалов (intervals).. Подскажите, что не так ?..


Корректный маппинг файла User.java :

Copy Source | Copy HTML
  1. @Entity
  2. @Table(name = "users")
  3. public @Data class User
  4. {
  5.  
  6.     // Base Mapping
  7.  
  8.     @Id
  9.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  10.     @Column(name = "user_id")
  11.     @Getter @Setter Long user_id;
  12.  
  13.     @Column(name = "user_name")
  14.     private @Getter @Setter String user_name;
  15.  
  16.     @Column(name = "login")
  17.     private @Getter @Setter String login;
  18.  
  19.     @Column(name = "password")
  20.     private @Getter @Setter String password;
  21.  
  22.     // Releations Mapping
  23.  
  24.     // Releations With Project
  25.  
  26.     @ManyToMany(cascade={CascadeType.ALL})
  27.     @JoinTable(name="users_on_projects",
  28.             [email protected](name="user_id"),
  29.             [email protected](name="project_id") )
  30.             private @Getter @Setter List projects = new Vector();

     
        @OneToMany
        @JoinTable(name="intervals",
                [email protected](name="users_on_projects_id"),
                [email protected](name="interval_id") )
                private @Getter @Setter List intervals = new Vector();
    }

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexey Pomogaev, 2011-06-21
@Foror

I can recommend to you to simplify communications by adding classes. For example, in your case, make the UserOnProject class and I think the problem will be resolved immediately. And later, perhaps this class will also come in handy for new attributes.

A
Alexey Pomogaev, 2011-06-21
@Foror

And in general, hibernate eats a lot of memory. Very dofiga, compared to the same solutions on django, for example, just orders of magnitude. Over time, I think about using something from NoSQL, the same MongoDB, to see what happens ...

A
asm0dey, 2011-06-22
@asm0dey

Sorry for the crooked suggestion, but you can do this:
Install Spring Roo, configure it for our database, then say database reverse engineer
Or NetBins can also generate database mappings.

A
asm0dey, 2011-06-22
@asm0dey

As an option, send me a database schema, I'll generate it myself.

I
Igor Petrov, 2011-06-19
@KriegeR

I'm sorry if I'm wrong (I haven't dealt with Gibernate for a very long time), but shouldn't mapping classes have a default constructor?

I
im_berzki, 2011-06-19
@im_berzki

Problem solved, thank you all for your help!
Correct description of the relationship I was thinking, maybe write an article about mapping complex relationships? And about mapping on annotations / a simple example? (like there isn't one) Users--->Intervals
@OneToMany
@JoinTable(name="intervals",
[email protected](name="users_on_projects_id"),
[email protected](name="interval_id") )
private @Getter @Setter List intervals = new Vector();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question