首页 \ 问答 \ 当子类只有@Transient字段时扩展@Entity的最佳方法(The best way to extend an @Entity when the subclass has just a @Transient field besides)

当子类只有@Transient字段时扩展@Entity的最佳方法(The best way to extend an @Entity when the subclass has just a @Transient field besides)

我有一个@Entity的超类,就像这样:

@Entity
@Table(name = "utente")
public class Utente implements Serializable{
   ...
}

除了以下内容之外,子类只有一个@Transient字段:

@Entity
public class UtenteSub extends Utente{
    @Transient
    private String newField;
}

为了使它工作,我应该添加@ @DiscriminatorValue ,@ @Inheritance并在表上添加一个字段。

这需要做很多工作,考虑到我在子类中所需要的只是一个@Transient字段(我需要它在表单中“提交”之后“检查”对象Utente)。

在我的场景中是否有更好更简单的方法来扩展@Entity

谢谢。


I have a superclass that is an @Entity, something like this one:

@Entity
@Table(name = "utente")
public class Utente implements Serializable{
   ...
}

The subclass has just a @Transient field besides:

@Entity
public class UtenteSub extends Utente{
    @Transient
    private String newField;
}

To make it work, I should add @DiscriminatorValue, @Inheritance and add a field on the table.

This is a lot of work to do, taking into account that all I need in the subclass is just a @Transient field (I need it to "check" the object Utente after its "submission" in a form).

Is there a better and easier way to extend the @Entity in my scenario?

Thank you.


原文:https://stackoverflow.com/questions/36128819
更新时间:2022-03-20 22:03

最满意答案

为什么不修改您的查询以包含与文章一致的标签? 我不确定这是不是你想要的,但是用逗号连接起来很简单:

SELECT 
   a.id, 
   a.nazwa, 
   a.data, 
   a.wstep, 
   a.imgs, 
   a.zdj,
   GROUP_CONCAT(t.name) all_tags
FROM 
   art a INNER JOIN tagarts ta ON a.id = ta.id INNER JOIN tags t ON t.idt = ta.idt
 WHERE id BETWEEN $end AND $start
 GROUP BY a.id
 ORDER BY a.id desc;

然后在PHP中进行拆分并瞧!

foreach($rows as $row) {
   foreach(explode(',', $row['all_tags']) as $tag) {
      echo '<a href="somepath/tags?t={$tag}">{$tag}</a>';
   }
}

这比使用一些查询而不是内部foreach要好得多。

在任何情况下,当您实际尝试在循环中执行这些查询时,所有内容都会中断的说法可能意味着您只是对代码存在一些问题。 尽管如此,即使它起作用,它仍然会很慢。

但实际上你可能需要一些完全不同的东西,在这种情况下不要杀了我而是别人。


Why not modifying your query to include tags in line with articles? I'm not sure if this is what you wanted, but it would be simple enough to concatenate it w/ commas:

SELECT 
   a.id, 
   a.nazwa, 
   a.data, 
   a.wstep, 
   a.imgs, 
   a.zdj,
   GROUP_CONCAT(t.name) all_tags
FROM 
   art a INNER JOIN tagarts ta ON a.id = ta.id INNER JOIN tags t ON t.idt = ta.idt
 WHERE id BETWEEN $end AND $start
 GROUP BY a.id
 ORDER BY a.id desc;

Then do a split in PHP and voila!

foreach($rows as $row) {
   foreach(explode(',', $row['all_tags']) as $tag) {
      echo '<a href="somepath/tags?t={$tag}">{$tag}</a>';
   }
}

This would be much more preferable than using some query instead of that inner foreach.

In any case, your remark that everything breaks when you actually try doing these queries in a loop probably means that you simply have some issues with your code. Still, even if it worked, it would work slow.

But you might actually need something completely different, in which case don't kill me but someone else.

相关问答

更多
  • 如果不管是哪个CMS都出错的话,可能php,mysql的问题, 建议使用AMP套件, LAMP的话,ubuntu server不错,ubuntu linux集成了apache,mysql,php WAMP套件也不少,appserv很好,XAMPP,easyphp貌似也不错, 我用的是appserv(php+apache+mysql+phpmyadmin) + windows xp 安装discuz,phpbb,phpwind,mediawiki都没出错, 或者是你自己的操作问题, appserv官方站点 h ...
  • 你最好将内容存储在数据库中。 请注意,您存储内容,而不是整个HTML页面(否则,您并不是真正构建内容管理系统)。 如果你使用纯文件来实现它,那么你必须发明一种文件格式来存储你的结构化数据,你必须弄清楚如何使它快速,担心数据完整性和竞争条件等等。 有了数据库,您可以完成所有这些工作,而且工作进展顺利。 在每个页面视图上执行数据库查询是很正常的事情; 实际上,网络应用通常在每个页面视图上执行超过5次和最多30次数据库查询,并且我猜测stackoverflow.com可能适合该范围。 MySQL足够快。 You' ...
  • 如果你不熟悉模型 - 视图 - 控制器模式,那么阅读它可能会比较谨慎。 MVC通常是Web应用程序高级设计的正确起点。 此外,CMS是一个足够大的项目,你应该考虑使用像CodeIgniter,Symfony,Zend等PHP框架来让你的生活更轻松。 它消除了许多苦差事和常见任务。 动态页面创建和显示 我想你想把它分成两件事:文本内容(基本上你在表单中放置的内容)和围绕该内容的HTML模板。 当您创建页面或博客文章时,您可能希望将实际内容(您在创建表单中键入的内容)存储在某种类型的数据库中(不一定是RDBMS ...
  • 这个问题有两种常见的解决方法,都涉及将可允许的操作拆分成您可以命名的离散事件。 (删除帖子,创建画廊,升级代码,转储数据库,启动导弹,打开车库门。)在位图中为每个位图提供一个位图(如果您打算拥有少于32个权限,这是一个很好的和紧凑的存储机制;如果你认为它会增长超过32个权限,一个典型整数的大小,那么可能是你的数据库中的一张表。) 因此,您可以直接将用户映射到权限(“我想授予gallery_create到sarnold ”),或者您可以通过用户类映射用户和权限(“我想授予gallery_create给art_ ...
  • 为什么不修改您的查询以包含与文章一致的标签? 我不确定这是不是你想要的,但是用逗号连接起来很简单: SELECT a.id, a.nazwa, a.data, a.wstep, a.imgs, a.zdj, GROUP_CONCAT(t.name) all_tags FROM art a INNER JOIN tagarts ta ON a.id = ta.id INNER JOIN tags t ON t.idt = ta.idt WHERE ...
  • 您必须echo或print变量才能显示它。 您还可以执行var_dump来检查值是什么。 <?php echo htmlspecialchars($title); ?> 我添加了htmlspecialchars以防止脚本注入,我不知道它是否可以在title标 ...
  • 你需要放慢一点; 起初看起来你的问题是关于动态生成HTML表单的js / php代码,但后来你开始讨论数据库字段。 你的问题对我来说仍然不是100%清楚,但我在你的情况下做的是从数据库设计开始,而不是专注于其他任何事情。 我可以看到你在考虑改变用户表。 我认为这不是必要的,也不是好的设计。 只要每个用户都有一个主键即users.id字段,您就可以创建一个新表来存储表单字段数据,如下所示: CREATE TABLE users_custom_form_data( id INT NOT NULL, FO ...
  • 以下是适合此任务的多对多关系的基本原则示例。 当一个表中的一个或多个行可能与另一个表中的多个行相关联时,多对多关系是两个表之间的设计模式。 两个表之间的关系存储在第三个表中,该表存储每个关系的主键( postID和categoryID列)。 数据库表的示例: post postID | postTitle | postDesc | postCont | postDate 1 Post 1 info1 content1 date1 2 Post 2 info2 c ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的