Campbell Ritchie wrote:That use of equals() looks dubious to me; maybe you have missed out some quote marks.
. . . .getString("A").equals(-3.82)). . . .
Deyan Perov wrote:
There will never be a third table when using @OneToMany / @ManyToOne / @OneToOne unless you explicitly tell JPA to use one. A unidirectional @OneToMany will still use a join column in the MANY table; the MANY entity just doesn't use it itself. With @JoinColumn you tell JPA explicitly what the column looks like instead of having JPA using its defaults.
I mean by this: https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/ Unidirectional @OneToMany
As you can see I am not conformable with JPA, Hibernate, and Relationships. Could you please suggest to me some good documentation where I can read about it? I am going to search over coderanch too, maybe I will find some interesting text which can help me not to be confused.
Deyan Perov wrote:Rob Spoor, I want to thank you for all your effort to help! But now when I scratched deeper I am still confused and now I have many more questions...
On a JPA level, the one without mappedBy is the owner. By default with @ManyToOne / @OneToMany, that means the MANY side.
I always think the owner is ONE because I thought MANY of something depends on that ONE.
It's possible to change the owner to Town, but that means that you should remove the (explicit) relationship from the Student side, basically creating a unidirectional relationship.
I this case it will be created a new table and I will have 3 tables instead of 2. This can be prevent with @JoinColumn?
@ManyToOne on Student. Town doesn't even need @OneToMany to get the foreign key; it's a unidirectional many-to-one without it.
@OneToMany without mappedBy on Town, and no @ManyToOne on Student. This is a unidirectional one-to-many.
@OneToOne with mappedBy on Student. Note that the non-owner side needs mappedBy with @OneToOne.
This is new to me... First I have to understand bidirectional and unidirectional. Bidirectional doesn't exist in relational databases, there exist only undirectional, right? Table A has a FK from Table B, but Table B doesn't have a FK from Table A, right? And this is undirectional?
We need to bidirectional on the level of Java because we need to have a reference for an object in a class. i.g. We have a Student which HAS-A Town, Town HAS-A student. Am I right? The student has to know for the Town, and the Town has to know for the Student.
But if the Town doesn't have a reference of the Student is that undirectional then?
Paul Clapham wrote:I gave you a cow for such a comprehensive answer, Rob. I also gave Deyan a cow for posting such a well-formed comprehensively-answerable question.
Dave Tolls wrote:
Deyan Perov wrote:
I have Town which can have many Students. Student(s) can exist without Town and Town can exist without Student(s).
Rob's covered everything, but I didn't notice whether he spotted this.
You say a Student doesn't have to have a Town...OK, but your StudentEntity has its TownEntity marked as NotNull.
Deyan Perov wrote:A town is the OWNER of a relationship.
To tell Hibernate who is the owner we use @OneToMany(mappedBy = "townEntity").
And this means in the Student table it will be created foreign key town_id, right?
@JoinColumn(name = "town_id") - I just don't see the difference when I have this and don't understand when to use it because I saw codes without it too. I read it about it but...Probably I missed something more from relationships to understand when I have to use it. I am not sure when I need it and what exactly does it when I "get" the same with mappedBy.
Before I ask about JSON annotations let me say about the other problem.
This works, when I POST Student it creates the Student and his Town. When I create another Student with the same Town name it has the id from that Town (id = 1) but when I change some attribute in Student and want to PUT it I get some changes. A new Town is created with the same name and has id = 2, the Student which I updated now has foreign key town_id = 2. Why?
I want to have to Create, Read, and Update a Town. I don't need it deletes. But when I change a Student and if I don't touch a Town information I want to stay the same.
To avoid infinite JSON loop I use and . But when I want to GET a Student it doesn't show my any data of his Town. I found this can help me but I am not sure is that what I need it
Because when I GET a Student Postman backs me the Student with his Town and the Town has an array of all Students who have FK of that Town.
Gerard Gauthier wrote:
Rob Spoor wrote:... Just remember that any project using the dependency cannot be built by others that don't have the dependency.
So basically(since the dependency is local) I can only build I it on my machine. Right?
I kind of figured out how dependencies work by exploring my ~/.m2 folder and the pom.xml's dependency section.
I just walked into my ./m2 folder using the tags for junit as steps.
Tim Holloway wrote:For proper I18N processing of general text from a file, I'd recommend that you open a FileInputStream, then construct an InputStreamReader on that. Or on System.in, if using stdin. The InputStreamReader allows you to set the proper charset for your input so that stuff like accented letters get handled properly and cleanly. Note that FileReader doesn't appear to have any characterset constructors itself.
Once you have a properly-configured Reader, I recommend using it to construct an LineNumberInputReader. This gives you (non-deprecated) readLine() methods plus if you need to complain about input, you'll have the offending line number already available without having to implement line-counting logic of your own.