Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using association in @Embedded entity

I have problem with doctrine embeddables and using associations in it. When i add @ManyToOne to embedded entity doctrine don't create address_city column in user table, however address_street column is created. I seams bit strange because there is no error, city is silently ommited.

Code look like this:

/** @Entity */
class User
{
    /** @Embedded(class = "Address") */
    private $address;
}

/** @Embeddable */
class Address
{
    /** @Column(type = "string") */
    private $street;

    /** @ManyToOne(targetEntity = "City") */
    private $city;
}

/** @Entity */
class City
{
    /** @Column(type = "string") */
    private $name;
}
like image 761
Yavin Avatar asked Jan 07 '15 10:01

Yavin


People also ask

How do I embed one entity inside another entity?

In this tutorial, we will learn how to embed one entity inside another entity, so they are mapped to a single table. With JPA and Hibernate we can use the @Embeddable annotation to mark a class to be eligible as an embeddable class. We can use the @Embedded annotation to embed an embeddable class.

How many embeddable entities can an entity have?

One entity can be embedded in another entity. The attributes of an entity can be common attributes of more than one entity. In this case there can be one embeddable entity.

How to override attributes of an embedded class in JPA?

The attributes of an embedded class can be overridden using the @AttributeOverrides and the @AttributeOverride annotations. @Embeddable annotation - JPA provides the @Embeddable annotation to declare that a class will be embedded by other entities. @Embedded annotation - The JPA annotation @Embedded is used to embed a type into another entity.

What is an associative entity?

The associative entity is always the child side of the relationships. These relationships show how two or more fundamental entities relate. The business terms, usually primary terms, that are the origin of the definition of this associative entity. A business term can be the source of one or more associative entities depending on its definition.


1 Answers

State for 2015.01.11:

This case is currently not supported in doctrine. It's now described in doctrine issue tracker here

Here is the explanation:

We don't support associations from embeddables right now...

It will probably not implemented for now, as embeddables (in our vision) are fitting the use-case of ValueObjects. ValueObjects are (usually) supposed to be containing serializable data, and an entity reference is not serializable data.

like image 149
Yavin Avatar answered Sep 21 '22 05:09

Yavin