发布于2021-06-05 09:09 阅读(1087) 评论(0) 点赞(24) 收藏(0)
我想知道是否有可能将多个表提取到一个 Java 类中,例如:
TABLE LABELS;
TABLE STANDARDS;
TABLE REFERENCES;
映射到同一个类
public Class Information {
private String type; // the type is the element who have to do the mapping => LABELS/STANDARDS/REFERENCES
...
}
由于技术原因,我不可能为每种类型构建一个类(我知道某些传统应该很酷)。
谢谢
吉尔斯
编辑 :
我会尝试多解释一点:)
我正在使用 JMS 服务来获取信息。每条消息都有特定的类型(在我的示例中:“标签”、“标准”和“参考”)。
通过使用这些类型,我想在相应的表中保留信息。每条消息的结构都完全相同,这就是我想要使用唯一 POJO 的原因。
我希望它更好地解释:)
编辑 2:
TABLE LABELS (
ID PRIMARY KEY AUTO_INCREMENT,
MESSAGE VARCHAR(255),
AUTHOR VARCHAR(255)
);
TABLE STANDARDS(
ID PRIMARY KEY AUTO_INCREMENT,
MESSAGE VARCHAR(255),
AUTHOR VARCHAR(255)
);
TABLE REFERENCES (
ID PRIMARY KEY AUTO_INCREMENT,
MESSAGE VARCHAR(255),
AUTHOR VARCHAR(255)
);
这里有一些 JMS 的例子
headers :
type : label
body:
{message:"name of the country",author:"john doe"}
headers :
type : label
body:
{message:"nom du pays",author:"jenny doe"}
headers :
type : reference
body:
{message:"country",author:"john doe"}
我想将它们放入信息类并将它们保存到正确的表中
试试这个:
@MappedSuperclass
public class Base {
private String message;
private String autor;
@Column(name = "MESSAGE")
public String getMessage(){
return message;
}
public void setMessage(final String message) {
this.message = message;
}
@Column(name = "AUTOR")
public String getAutor(){
return autor;
}
public void setAutor(final String autor) {
this.autor = autor;
}
}
和三个类:
@Entity
@Table(name="LABELS")
public class Labels extends Base{};
和
@Entity
@Table(name="STANDARDS")
public class Standards extends Base{};
和
@Entity
@Table(name="REFERENCES")
public class References extends Base{};
现在您可以使用以下方法持久化数据:
Base b;
if (info.getType().equals("REFERENCES")) {
b=new References();
} else if (info.getType().equals("LABELS")) {
b=new Labels();
} else if (info.getType().equals("STANDARDS")) {
b=new Standards();
} else {
return;
}
b.setMessage(info.getMessage());
b.setAutor(info.getAutor());
Transaction t = session.beginTransaction();
session.persist(b);
t.commit();
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/216202/b6618849c42f4166b78e/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!