0

Hello everyone I am trying to get my class to persist to my database however I have run into a problem im not sure how to solve. I am attempting to map the three instances of Score with my user but I am not sure how ti get this to work. Any help is appreciated. Thank you :)

User class Snipit.

@Column(name="userName", unique=true)
private String username;
@Column(name="password")
private String password;

//I am not sure how to map the three fields below.
private Score highestScore; 
private Score averageScore; 
private Score lowestScore;
Devin Wall
  • 180
  • 1
  • 16
  • Is it Score anyway diferent from common number? If so you should make mapping -> onetone, manytone etc... – Milkmaid Dec 08 '14 at 08:54
  • Yes it holds words per minute and the errors – Devin Wall Dec 08 '14 at 08:54
  • This might be helpfull [@OneToOne](http://stackoverflow.com/questions/21762328/java-hibernate-onetoone-mapping) – Milkmaid Dec 08 '14 at 08:56
  • I am aware of that but im not sure what type of relationship to use here. I think its one to one but im not sure what feilds to map on. as the score just has two ints – Devin Wall Dec 08 '14 at 08:58

3 Answers3

1

While there is an accepted answer here I am not seeing how the proposed mapping distinguishes between the High, Low and Average scores?

You could consider using a Map such as below where the score table will have a column 'score_type' which can be mapped to an Enum 'ScoreType':

public class User{  

    @Column(name="userName", unique=true)
    private String username;

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

    @OneToMany
    @MapKeyColumn(name="score_type")
    @MapKeyEnumerated
    private Map<ScoreType, Score> scores; 

    public Score getHighScore(){
        return scores.get(ScoreType.HIGH);
    }

    public void setScore(Score score){
        score.setUser(this);
        scores.put(score.getType(), score);
    }
}

and

public class Score{

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @Column(name = "type")
    @Enumerated(EnumType.String)//or ordinal
    private ScoreType type;
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
0

if score not related to the database, use @Transient annotation for score

if score related to database, use mapping annotation such as @OneToOne , @OneToMany (depend on your database cardinality)

EDIT : if score related to database table, IMHO the easiest way is let the IDE automaticly generate entity from database for you (but sometimes IDE can generate undesired mapping, so you still need to check it)

if you using eclipse Generating entities from tables

if you using netbeans Adding Entity Classes

but you still need to know about Multiplicity in Entity Relationships

user3598272
  • 145
  • 10
  • I want the table to have the three columns so its one to one... how ever im not sure how to map this... – Devin Wall Dec 08 '14 at 09:24
  • There has to be an intermediary Table doesn't there – Devin Wall Dec 08 '14 at 09:32
  • @Devin, why do you need an intermediary table? You can just reference Score table from Snipit unless there's a ManyToMany relation. – iozee Dec 08 '14 at 09:35
  • @iozee because there would need to be 6 columns then in that table. Two columns per score. with three scores. Seems better to delegate it one to one -> x -> one to many – Devin Wall Dec 08 '14 at 09:45
0

In my point of view User have one score and score have one user. So @OneToOne User is the owning side and its bidirectional.

code should looks like this

in Score:

@OneToOne(mappedBy = "score")
private User user

in User:

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@PrimaryKeyJoinColumn
private Score highestScore;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@PrimaryKeyJoinColumn
private Score averageScore;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@PrimaryKeyJoinColumn
private Score lowestScore;
Milkmaid
  • 1,659
  • 4
  • 26
  • 39