如何从Hibernate @MappedSuperclass覆盖一种方法但保留原始映射? [英] How can one override a method from a Hibernate @MappedSuperclass but keep the originial mapping?

查看:117
本文介绍了如何从Hibernate @MappedSuperclass覆盖一种方法但保留原始映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射的超类,用于定义持久性映射的方法.我正在使用每个类的表继承策略.例如,这是一些简单的类继承:

I have a mapped super class that I use to define a method it's persistence mapping. I am using the table-per-class inheritance strategy. For example here is some simple class inheritance:

@MappedSuperclass
public class Feline {
    private Long id;
    private String identifier;

    @GeneratedValue(strategy = "GenerationType.AUTO")
    @Id
    public Long getId() {
        return id;
    }

    public String getIdentifier() {
        return identifier;
    }
    // other getters and setters, methods, etc.
}

下一类不会覆盖getIdentifier()方法,并将Cat的标识符保存在其实体表的标识符"列中.

The next class does not override the getIdentifier() method and saves the Cat's identifier in the "identifier" column of it's entity table.

@Entity
public class Cat extends Feline {
    private Date dateOfBirth;
    private Set<Kitten> kittens;

    @OneToMany(mappedBy = "mother")
    public Set<Kitten> getKittens() {
        return kittens;
    }
    // other getters and setters, methods, etc.
}

在Kitten类中,我想更改标识符以返回Kitten.identifier +的小猫" + mohter.getIdentifier()或例如"Ada的Boots小猫",并将此String保留在实体表.

In the Kitten class I want to change the identifier to return the Kitten.identifier + " kitten of " + mohter.getIdentifier() or for example "Boots kitten of Ada" and persist this String in the "identifier" column of the entity's table.

@Entity
public class Kitten extends Cat {
   private Cat mother;

   @ManyToOne
   public Cat getMother() {
       return mother;
   }

   @Override
   public String getIdentifier() {
       return this.getIdentifier() + " kitten of " + mother.getIdentifier(); 
   }
}

当我运行此代码时,我收到一条错误消息原因:org.Hibernate.MappingException:在com.example.Kitten中发现的标识符的重复属性映射."

When I run this code I get an error message "Caused by: org.Hibernate.MappingException: Duplicate property mapping of identifier found in com.example.Kitten."

由于我正在扩展@Mappedsuperclass,因此标识符字段应该映射到每个实体表的"identifier"列,但是由于某种原因,这种情况不会发生,并且当我覆盖getIdentifier( )中的方法.

Since I am extending a @Mappedsuperclass the identifier field should be mapped to the "identifier" column of each entity table but for some reason this doesn't happen and it tries to map the identifier field twice when I override the getIdentifier() method in the Kitten class.

Cat和Kitten表均具有标识符"列.我不明白为什么如果方法返回正确的类型以映射到同一列,那么我无法覆盖该方法.

Both the Cat and Kitten tables have "identifier" columns. I do not understand why I cannot override a method if it returns the correct type to map to the same column.

推荐答案

那是行不通的.但是您可以定义一个鉴别器.

That won't work. But you can define a discriminator.

请查看 http://docs. jboss.org/hibernate/orm/3.3/reference/en/html/inheritance.html

这篇关于如何从Hibernate @MappedSuperclass覆盖一种方法但保留原始映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆