3

I think I understood what the attribute mappedBy means if put in a @OneToMany field (i.e. the table representing the type of the field has a foreign key to this table that declared @OneToMany(mappedBy="...") , but I don't understand exactly its syntax (or at the end its meaning -> contradiction).

According to the documentation:

mappedBy

public abstract String mappedBy

The field that owns the relationship.

Required unless the relationship is unidirectional.

Default: ""

Which field is the documentation talking about? What should the value of mappedby match, and in which table?

Community
  • 1
  • 1
nbro
  • 15,395
  • 32
  • 113
  • 196
  • here's one explanation http://stackoverflow.com/a/9108618/3166303 – leeor Apr 23 '16 at 00:01
  • @leeor As I'm saying in my question, I think I understood the meaning of the purpose of the attribute but not its syntax, which values can `mappedby` assume? Do those values need to match which attributes and in which tables or models? – nbro Apr 23 '16 at 00:08
  • Ah, I see. The attributes need to match the field names in the annotated classes participating in the relationship, not the database table column names. – leeor Apr 23 '16 at 00:09
  • @leeor Ok, but the documentation only says one field (not many as you're saying). This field is the field intended as normally intended a field of a class in Java or what? I've seen around that usually the value of `mappedby` is simply the name of the class (but in lowercase) that is using the `mappedby`... so, around (at least in a few places that I've seen) people are not using any field's name of any table participating in the relationship, but they seem pretty random names at most... – nbro Apr 23 '16 at 00:15
  • 1
    no its refers to an actual instance field in the class that participates in the relationship. I'll try to elaborate in an answer now that I understand your question better. – leeor Apr 23 '16 at 00:19

1 Answers1

3

Check out this example. There are two classes involved in the one-to-many relationship in this example: Stock and StockDailyRecord. Notice the @OneToMany stockDailyRecords field in class Stock:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
public Set<StockDailyRecord> getStockDailyRecords() {
    return this.stockDailyRecords;
}

So in this case, its saying that the field stock in the StockDailyRecord class (not to be confused with the class Stock) owns the relationship. What I think makes this more confusing is that in this case, both the name of the field and the class are the same. This sort of case is also very common, since you tend to refer to the name of the relationship on the other side by the lowercase of the class name of that field by convention (e.g. stock for Stock).

So the mappedBy attribute is actually owned by the StockDailyRecord class. So that means that the StockDailyRecord will handle persisting the stockDailyRecords referenced in the Stock class.

The name referenced in the mappedBy attribute value is a class field name, not a table column name.

And this is how the StockDailyRecord side of that relationship looks:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STOCK_ID", nullable = false)
public Stock getStock() {
    return this.stock;
}

Hope this helps, I know its confusing :)

leeor
  • 17,041
  • 6
  • 34
  • 60
  • Thanks! I think now it's clear. I would have though another question or doubt. The name attribute in `@JoinColumn` indicates the name of the foreign key column in the `StockDailyRecord`, right? Another question: as things are set now, `Stock` does not have any column referencing a `StockDailyRecord`, right? Last question: how would you add a foreign key to `Stock` that also references `StockDailyRecord`? Or is it even a good idea? Sorry, man, I just would like to understand very well, but reading around usually only confuses you.. – nbro Apr 23 '16 at 00:33
  • also because people seem to assume you know a few important details. – nbro Apr 23 '16 at 00:34
  • On your first question, yes - that is a database column name. – leeor Apr 23 '16 at 00:36
  • for the second question: that's right. the foreign key in the database resides in the `stock_daily_record` table, as that's all you need to capture the relationship. – leeor Apr 23 '16 at 00:37
  • you don't need a foreign key on both sides, as its the foreign key in the stock_daily_record table is sufficient to capture the relationship. – leeor Apr 23 '16 at 00:39
  • updated it slightly to try to make it a bit more clear. – leeor Apr 23 '16 at 00:49