Is it possible to reference a column other than 'id' for a JoinColumn?
I think Doctrine wants these to be primary keys, from the docs:
Another thing that jumps out at me from your code sample is category.id2 being type string, I would at least expect it to be an integer, but it may also need to be for @JoinColumn to work properly.name: Column name that holds the foreign key identifier for this relation.
You may be able to get away with just @Index on category.id2 and leave it as a string though; worth a shot anyway.
Join column from another table with Foreign Key using JPA
I don't really understand this. I'm sure @JoinColumn
can accomplish the behavior you're looking for.
I was looking into @JoinColumn but was not seeing the result I wanted since the foreign key is a different column in my EntityExample:
@Entity
@Table(name = "jobs")
public class KronosFileEntity {
@Id
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumn = "id")
private User user;
}
Then you can access the email like job.getUser().getEmail()
Or add a convenience method if that helps
public String getUserEmail() {
return user.getEmail();
}
Thenjob.getUserEmail()
Join columns by referencedColumnName that is not primary key - id
You have mixed up the column-name and the field-name that shall be referenced in your @JoinColumn
declaration.
@JoinColumn(name="id", referencedColumnName="user_id")
This way Doctrine looks for a field/property named user_id
on your User
entity. I guess you want the column in the join-table to be named user_id
and the entries being id
's of the User
entity.UserDetail
/**
* @ORM\Entity
*/
class UserDetail
{
/**
* @ORM\ManyToOne(
* targetEntity="User",
* inversedBy="details"
* )
* @ORM\JoinColumn(
* name="user_id",
* referencedColumnName="id"
* )
*/
protected $user;
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/** @ORM\Column() */
protected $key;
/** @ORM\Column() */
protected $value;
public function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}
Userclass User
{
/**
* @ORM\Id()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\OneToMany(
* targetEntity="UserDetail",
* mappedBy="user",
* cascade={
* "persist",
* "remove",
* "merge"
* },
* orphanRemoval=true
* )
*/
protected $details;
public function __construct()
{
$this->details = new ArrayCollection();
}
public function addDetail(UserDetail $detail)
{
$detail->setUser($this);
$this->details->add($detail);
return $this;
}
Now if you add a detail to your User
like this and persist/flush afterwards:$user->addDetail(new UserDetail('Height', '173cm'));
This will result in a join-colum in the user_detail
table that looks like this:| key | value | user_id |
|---------------|-----------|---------|
| Height | 173cm | 1 |
JPA : relation with other column than id
Use the referencedColumnName
attribute of the @JoinColumn
annotation. I'm not sure this is supported by the standard though. It might work in some implementations, and not in others.
See http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association for Hibernate, and http://openjpa.apache.org/builds/2.1.0/apache-openjpa-2.1.0/docs/manual/manual.html#ref_guide_mapping_notes_nonstdjoins for OpenJPA.
Hibernate select id of join column without join
You can map the foreign key to the entity twice in this case, one for actual ORM and another for getting the FK without actually firing a new query.
public class Answer {
@JoinColumn(name = "question_id")
@ManyToOne(targetEntity = Question.class, fetch = FetchType.LAZY)
private Question question;
@Column(name = "question_id", insertable = false, updatable = false)
private Long questionId;
}
Here question_id
is present in the answer
table.This way that foreign key will be already available in the result of the first query(in the questionId field) and the new query won't be fired for getting the FK value.
What is referencedColumnName used for in JPA?
It is there to specify another column as the default id column of the other table, e.g. consider the following
TableA
id int identity
tableb_key varchar
TableB
id int identity
key varchar unique
// in class for TableA
@JoinColumn(name="tableb_key", referencedColumnName="key")
Related Topics
PHP Curl Post Returns a 415 - Unsupported Media Type
How to Send HTML Mails Using Pear Mail
Share Variables/Memory Between All PHP Processes
Login Event Handling in Laravel 5
PHP - What's the Benefit of Unsetting Variables
Change Xml Node Element Value in PHP and Save File
(When) Should I Use Type Hinting in PHP
Compare Object Properties and Show Diff in PHP
How to Encrypt HTML Source Code Output Using PHP
Maximum Function Nesting Level of '100' Reached, Aborting After Upgrading to Laravel 5.1
Phpexcel Get Formatted Date as Is Visible in Excel File
How to Detect If a Database Table Exists with Laravel
PHP to Easyphp MySQL Server 1 Second Connection Delay
Codeigniter 3 - Access Session from Outside Codeigniter Installation
Does Die() End Your Session in PHP