使用现有表的Hibernate @ElementCollection和@CollectionTable问题

时间:2018-07-14 11:03:46

标签: java hibernate

我有以下类别(产品图片)及其表( 产品 image -表已经存在于数据库中),我希望Product拥有Image的集合。图像的生命周期与产品相关,因此如果没有产品,则图像不应存在。

我决定在Image类上使用@ElementCollection@CollectionTable@Embeddable,但是在使其工作时遇到了问题。这些表已经存在于数据库中。我不希望Hibernate生成它们。

产品类别:

@Entity
@Table(name="product"/*, schema=""*/)
public class Product {

    //private static final String TABLE_SCHEMA = "";

    private static Logger logger = LogManager.getLogger("Product.class");


    @Id
    @Column(name="productid", updatable=false, nullable=false)
    private String id = IdGenerator.createId(); 

    @Column(name="name", nullable=false)
    @NotNull
    private String name;

    @Column(name="price", nullable=false)
    private double price;

    @Column(name="newPrice", nullable=false)
    private double newPrice;

    @Column(name="discount")
    private int discount;

    @Column(name="url")
    private String url;

    @Column(name="description")
    private String description;

    @ElementCollection(fetch=FetchType.LAZY)
    @CollectionTable(name="image)
    private List<Image> images = new ArrayList<Image>(); 

    public Product(){ }

    // GETTERS AND SETTERS HERE
}

图像类及其超类:

@MappedSuperclass
public abstract class MediaFile {

    @Id
    @Column(name="id")
    private String id = IdGenerator.createId();

    @Column(name="title", nullable = false)
    protected String title;

    @Column(name="filename", nullable = false)
    protected String filename;

    @Column(name="path", unique = true)
    private String path;

    @Column(nullable = true)
    protected int width;

    @Column(nullable = true)
    protected int height;

    public MediaFile(){
    }

   // GETTERS AND SETTERS HERE
}

//@Entity
@Embeddable
@Table(name="image")
@AttributeOverride(name = "id", column = @Column(name = "imageId"))
public class Image extends MediaFile{

    public Image(){}

    public Image(String title, String fileName, String url) {
        super(title, fileName, url);
    }
}

表格:

CREATE TABLE product(
    productId varchar(255) NOT NULL,
    categoryId varchar(255) NOT NULL,
    name varchar(255) NOT NULL,
    price DOUBLE NOT NULL,
    newPrice DOUBLE NOT NULL,
    discount INT,
    url varchar(255) NOT NULL,
    description varchar(255),
    images varchar(255) DEFAULT NULL,
    PRIMARY KEY (productId),
    FOREIGN KEY (categoryId) REFERENCES category(categoryId)
);

CREATE TABLE image(
    imageId  varchar(255) NOT NULL, 
    filename varchar(255) NOT NULL,
    title varchar(255) DEFAULT NULL, 
    path varchar(255) DEFAULT NULL,
    width int,
    height int,
    PRIMARY KEY (imageId, filename),
    FOREIGN KEY (imageId) REFERENCES product(productId) ON DELETE CASCADE
);

邮递员请求示例:

{
    "name": "Polo t-shirt",
    "price": 10,
    "newprice": 8,
    "discount": 30,
    "url": "www.example.com/shirts",
    "description": "Very nice products",
    "images": [{"title": "shirt", "filename": "shirt.jpg"}, {"title": "shoe",  "filename": "shoe.jpg"}]
}

运行应用程序时的异常:我在Image类上使用@Entity和@Embeddable测试应用程序。

1)在Image类顶部使用@Entity运行时,这是一个例外:

javax.persistence.EntityNotFoundException: Unable to find com.shopcheap.model.Image with id 2ae79753-e683-4701-8c9f-6fbf31326971
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:144) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...
...

id = 2ae79753-e683-4701-8c9f-6fbf31326971是由 {strong> MediaFile 类中的private String id = IdGenerator.createId();有意义,但该异常会抱怨找不到图像

com.shopcheap.model只是程序包名称。

1)在Image类顶部使用@Embeddable运行时:Product的图像不必具有自己的标识,因此我希望它们带有可嵌入的注释。这是我得到的例外:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Champ 'product0_.imageId' inconnu dans field list
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_151]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_151]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_151]

Champ 'product0_.imageId' inconnu dans field list被翻译为

Field 'product0_.imageId' unknown in field list

它表示找不到字段product0_.imageId。

我正在寻找将@ElementCollection与现有表格示例结合使用的教程。我发现的所有示例都让Hibernate自动生成了表,而这不是我想要的。我的表格(产品和图片)已经存在。

0 个答案:

没有答案