原则 2:级联持久化 Oracle "IDENTITY"返回 0 作为上次插入的 ID



我在oracle中使用条令2,数据库中的表有一些生成ID的触发器,我的表的ID映射如下:

/**
 * @ormId
 * @ormColumn(type="integer");
 * @ormGeneratedValue(strategy="IDENTITY")
 */
protected $id;

我有一个OneToMany关系,使用cascade={"persist"},但它不起作用,我在MySQL中尝试了相同的代码,它运行得很好,但在oracle中,最后一个插入Id似乎总是返回0,而不是插入行的实际Id。。。所以级联持续不起作用。。。这是教条上的错误还是我做错了什么?有什么帮助吗?

在遵循代码之后,似乎方法

DoctrineORMIdIdentityGenerator::generate

返回0,我不知道为什么要调用它,因为sequenceName为空(定义中没有序列!

编辑:以下是实体:客户实体:

/** 
 * @ORMEntity 
 * @ORMTable(name="clients")
 **/
class Client {
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     * @ORMColumn(type="integer")
     */
    protected $id;
    /** @ORMColumn(name="name",type="string",length=255,unique=true) */
    protected $name;
    /**
    * @ORMOneToMany(targetEntity="ContactInformation", mappedBy="client", cascade={"persist"})
    **/
    protected $contactInformations;
    public function __construct() {
        $this->contactInformations = new ArrayCollection();
    }
    public function getId() {
        return $this->id;
    }
    public function getName() {
        return $this->name;
    }
    public function setName($name) {
        $this->name = $name;
        return $this;
    }
    public function getContactInformations() {
        return $this->contactInformations;
    }
    public function addContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient($this);
            $this->contactInformations->add($contactInformation);
        }
    }
    /**
     * @param Collection $tags
     */
    public function removeContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient(null);
            $this->contactInformations->removeElement($contactInformation);
        }
    }
    public function setContactInformations($contactInformations) {
        $this->contactInformations = $contactInformations;
        return $this;
    }
}

联系信息实体:

/** 
 * @ORMEntity 
 * @ORMTable(name="contact_informations")
 **/
class ContactInformation {
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     * @ORMColumn(type="integer")
     */
    protected $id;
    /**
     * @ORMOneToOne(targetEntity="ContactInformationType")
     * @ORMJoinColumn(name="type_id", referencedColumnName="id")
     **/
    protected $type;
    /** @ORMColumn(type="text") */
    protected $value;
    /**
     * @ORMManyToOne(targetEntity="Client", inversedBy="contact_informations")
     * @ORMJoinColumn(name="client_id", referencedColumnName="id")
     **/
    private $client;
    public function getId() {
        return $this->id;
    }
    public function getType() {
        return $this->type;
    }
    public function setType($type) {
        $this->type = $type;
        return $this;
    }
    public function getValue() {
        return $this->value;
    }
    public function setValue($value) {
        $this->value = $value;
        return $this;
    }
    public function getClient() {
        return $this->client;
    }
    public function setClient($client = null) {
        $this->client = $client;
        return $this;
    }
}

Oracle不支持自动递增,因此您不能在Doctrine中使用"IDENTITY"策略。您必须使用"SEQUENCE"(或"AUTO")策略。

当指定"AUTO"时,Doctrine将对MySql使用"IDENTITY",对Oracle使用"SEQUENCE"。

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-生成策略

最新更新