Reputation: 156
I want to have hibernate generate some tables with foreign keys and so on. Ill give you an example of the query i want hibernate to generate:
create table RealtimeCost(id INTEGER not null primary key Autoincrement,
mnemonic varchar(50)not null references Exchange(mnemonic),
sid int not null references License(sid),
price numeric(10,2) not null)
so this query should be generated by hibernate via Annotations. The corresponding class to this is:
@Entity
@Table
public class RealtimeCost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@MapsId("mnemonic")
@JoinColumn(referencedColumnName="sid")
private String mnemonic;
@MapsId("sid")
@JoinColumn(referencedColumnName="sid")
private Integer sid;
@Column
private Double price;
Example for what the mnemonic
in RealtimeCost
should be mapped to (each mnemonic in RealtimeCost
has exactly 1 value in Exchange
):
@Entity
@Table
public class Exchange {
@Id
@Column(name="mnemonic")
private String exchange;
@Column
private String description;
As you can see I've tried a bit with the help of the docs, but I was not able to have the foreign keys be generated by hibernate. It would be really kind, if anyone could tell me the needed annotations and values for this class, so i can do it myself for the other classes as well. Also please tell me if i need to change anything in the Exchange
class for the mapping to work. Thanks in advance
Upvotes: 0
Views: 2477
Reputation: 156
Every answer posted here got an upvote from me, because everyone was kinda right, but it was not 100% what i was searching for, yet it helped me solving my problem by myself. For the example i posted, the solution i was seeking is as follows (i also added not nullable):
@Entity
@Table
public class RealtimeCost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "mnemonic",nullable=false)
private Exchange exchange;
@ManyToOne
@JoinColumn(name = "sid",nullable=false)
private License license;
@Column(nullable=false)
private Double price;
these are the annotations i was seeking for RealtimeCost
class. I did not need any special annotations in Exchange class. @Nico answer was closest to what i need, therefore his answer will be accepted
Upvotes: 1
Reputation: 1294
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "accommodation_type", unique = true, nullable = false)
private AccommodationType accommodationType;
@ManyToOne()
creates a relationship according to @JoinColumn()
name
in @JoinColumn()
is the table name that you want to make a connection.
Then when you create a class that is going to be connected to main class, you first need to give it a table name below @Entity
e.g @Table(name="accommodation_types")
Then you create your variable.
//bi-directional many-to-one association to Accommodation
@OneToMany(mappedBy="accommodationType", fetch=FetchType.EAGER)
private List<Accommodation> accommodations;
value of mappedBy
is the variable name in main class.
Upvotes: 2
Reputation: 1932
You should set up the association in one entity and use the mappedBy
in the other. You don't need @MapsId
because you are not using embedded entities (read the docs). Take a look at the @OneToMany
and @ManyToOne
relationships:
@Entity
@Table
public class RealtimeCost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany
@JoinColumn(name="mnemonic")
private Exchange exchange;
...
}
@Entity
@Table
public class Exchange {
@Id
@Column(name="mnemonic")
private String mnemonic;
@Column
private String description;
@ManyToOne(mappedBy="exchange")
private RealtimeCost realtimeCost;
...
}
Upvotes: 1
Reputation: 1813
I'm not an expert but we let hibernate do all the work with the javax.persistence annotations for joining entities.
@javax.persistence.ManyToOne( fetch = javax.persistence.FetchType.EAGER, optional = true )
@javax.persistence.JoinColumn( name = "VIEWTYPE_ID", nullable = true, unique = false, insertable = true, updatable = true )
private com.company.other.subproject.ViewType viewType;
Maybe this is what you need. Since this let's hibernate care about the tables that have to be created or not and the foreignKeys get created automatically with the dialect of the database you communicate with.
Upvotes: 1