Normally to realized linked objects, i usually use getter and setter methods and this way i add objects of different type to another object.
Now i have come across this Ruby Structure, e.g:
class Article < ActiveRecord::Base
has_many :comments
end
class Comments < ActiveRecord::Base
belongs_to :article
end
Can you tell me what is the equivalent of this has_many
and belongs_to
in Java. Basically i want to translate some similar data structure from Ruby to Java.
Highly depends on the ORM you're using. Most (I assume) people will go with Hibernate in java. With Hibernate you have the possibility to annotate relationships quite similar.
@OneToMany
and @ManyToOne
annotations seem to be those which you might have to take a closer look at.
Article class:
public class Article {
@OneToMany(mappedBy = "belongsTo")
private List<Comment> comments;
[...]
}
Comment class:
public class Comment {
@ManyToOne
private Article belongsTo;
[...]
}
If you want to use another ORM, I'm afraid I can't help you out :)