JPA - 没有关系的坚持实体

时间:2017-10-27 13:07:23

标签: java hibernate jpa spring-data-jpa persistence

这是一个非常基本的问题,但我找不到解决问题的正确文档部分。我正在编写一个简单的POC来学习Spring / JPA,以便重写一个应用程序。我的一个POJO看起来像这样:

<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">DESIGN</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">Home</a></li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="services.html">Services<span class="caret"></span></a>

<ul class="dropdown-menu">
      <li><a href="logo.html">logo design</a></li>
      <li><a href="banner.html">Banner Design</a></li>
      <li><a href="psd.html">Psd Desgin</a></li>
    </ul>
  </li>
  <li><a href="contact.html">contact us</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
  <li><a href="index.html"><span><img src="img/fr.png" height="20px" width="20px"></span>English</a></li>
  <li><a href="fr_index.html"><span><img src="img/eng.png"  height="20px" width="20px"></span> French</a></li>
</ul>
</div>
</nav>
<div class="container">
<h1>Hello Listen Dear !!!!</h1>
<p>Why i listen you?</p>
</div>






french language page
link here
fr_index


<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">CONCEPTION</a>
</div>
<ul class="nav navbar-nav">
  <li class="active"><a href="fr_index.html">Accueil</a></li>
  <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="fr_services.html">Prestations de service<span class="caret"></span></a>
    <ul class="dropdown-menu">
      <li><a href="fr_logo.html">création de logo</a></li>
      <li><a href="fr_banner.html">Conception de bannière</a></li>
      <li><a href="fr_psd.html">Psd Design</a></li>
    </ul>
  </li>
  <li><a href="fr_contact.html">Contactez nous</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
  <li><a href="index.html"><span><img src="img/fr.png" height="20px" width="20px"></span>English</a></li>
  <li><a href="fr_index.html"><span><img src="img/eng.png"  height="20px" width="20px"></span> French</a></li>
</ul>
</div>
</nav>
<div class="container">
<h1>Bonjour Cher Ecoute !!!!</h1>
<p>Pourquoi je t'écoute?</p>
</div>

如何指定如何保​​留路径属性?如果它是一个字符串,那将是显而易见的,但此刻,我得到一个例外。我理解为什么,但不知道如何绕过它。

@Entity
@Table(name = "Image")
public class EntityImage {

/**
 * The id of the image.
 */
@Id
@NotNull
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

/**
 * Path to the image.
 */
@Column(name = "path")
private Path path;

/**
 * Type of the image.
 */
private ImageType type;
...

我写的用于执行持久性的小测试如下(改编自springboot快速启动示例)

org.hibernate.MappingException: Could not determine type for: java.nio.file.Path, at table: image, for columns: [org.hibernate.mapping.Column(path)]

存储库如下:

public static void main(final String[] args) {
    SpringApplication.run(EntityImagePersister.class);
}

@Bean
public CommandLineRunner demo(final EntityImageRepository repository) {
    return (args) -> {
        // save a couple of customers
        final File file = new File("H:\\ZModel.png");
        final Path p = file.toPath();

        repository.save(new EntityImage(1L, p, ImageType.AVATAR));

1 个答案:

答案 0 :(得分:1)

使用自定义转换器与found=$(grep -Fx "$line" "$file") - 注释。

您的转换器可能如下所示:

javax.persistence.Convert

你的POJO中的字段就像那样:

class PathConverter extends javax.persistence.AttributeConverter<Path, String>{

     @Override 
     public String convertToDatabaseColumn(Path path){
         return /* your convert operation from path to string */;
     }

     @Override 
     public Path convertToEntityAttribute(String string){
         return /* your convert operation from string to path */;
     }
}

通过这种设置,每当你坚持你的POJO时,转换器被调用以从路径获取字符串,反之亦然,当从数据库字符串加载转换为路径时