首页 \ 问答 \ JPA / HIBERNATE:值必须是常量(JPA/HIBERNATE: Value must be a constant)

JPA / HIBERNATE:值必须是常量(JPA/HIBERNATE: Value must be a constant)

我有以下情况。

public enum TestEnum{

    PUUNITONE("puunitone"),
    PUUNITTWO("puunittwo");

    private String name;

    private TestEnum(String name) {
        this.name = name;
    }


    public String getName() {
        return name;
    }

}

EJB @stateless类中

@PersistenceContext(unitName = TestEnum.PUUNITONE.getName())
private EntityManager entityManager;

我有以下几点

Compilation Error: Value must be a constant

现在我的问题:

1.在java中是不是枚举常量? 如果是,这里的问题是什么?
2.使用Enum作为常量来解决这个问题还有办法或工作方法吗?

非常感谢任何建议


I have the following case.

public enum TestEnum{

    PUUNITONE("puunitone"),
    PUUNITTWO("puunittwo");

    private String name;

    private TestEnum(String name) {
        this.name = name;
    }


    public String getName() {
        return name;
    }

}

in the EJB @stateless class

@PersistenceContext(unitName = TestEnum.PUUNITONE.getName())
private EntityManager entityManager;

I have the following

Compilation Error: Value must be a constant

Now my question:

1. In java are enums constants or not? If yes what is the problem here?
2. Is there a way or workarround to solve this issue by using Enum as constant?

Thanks a lot for any suggestion


原文:https://stackoverflow.com/questions/19702001
更新时间:2024-03-27 11:03

最满意答案

我想在处理验证码时无法避免处理渲染图像? 这是一个简单的(可能不是最优雅的):

样本输出

session_start();

$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{ 
    $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
    $i++;
} 

$_SESSION['captcha'] = $code;

//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 200, 200, $background);

// use your own font!
$font = 'monofont.ttf';

//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);     

// prevent client side  caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

以一种形式显示它:

<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">

提交后,请检查输入的代码:

if ($_POST['captcha'] == $_SESSION['captcha'])
    // do your thing

I guess it can't be avoided to deal with rendering an image when dealing with captchas? Here's a simple one (and may not be the most elegant):

sample output

session_start();

$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{ 
    $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
    $i++;
} 

$_SESSION['captcha'] = $code;

//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 200, 200, $background);

// use your own font!
$font = 'monofont.ttf';

//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);     

// prevent client side  caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

Display it in a form:

<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">

Once submitted, check the entered code:

if ($_POST['captcha'] == $_SESSION['captcha'])
    // do your thing

相关问答

更多
  • 如果我理解正确,这个任务不需要验证码。 我假设你想看看用户是否点击自己,坐在他的电脑前。 新想法 将多个图片提交到您的表单上: ... 当生成表单时,获取1到8之间的随机数并将其保存在$_SESSION['submitnumber'] 。 创建两个相同大小的图像 - 一个空的填 ...
  • 验证码图像的目的是提出一些机器不可读的东西(至少很容易),以防止脚本小子,行为不端的机器人和恶意黑客对网站的基础设施造成严重破坏。 虽然有很多解决方案可以用Perl创建验证码图像,但是没有任何解读它们的方法。 有一个模块, WebService :: Antigate使用外部源来分析验证码,但这样做几乎肯定会违反目标站点的TOS。 这可能(也可能应该)让你在某些时候遇到麻烦。 小心谨慎,并确保获得事先授权(祝你好运!):) The point to captcha images is to come up ...
  • 看起来像validateCapture的数据发送被破坏了。 目前你在AJAX调用中有这个: data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField, 这导致调试变量转储: Array ( [recaptcha_challenge_field] => 03AHJ_VuskQR9VZwr6Uo5DllF4CL1GHDLajHiTQGHI3SE2p ...
  • 通常我使用这样的div: 有了Jquery,你可以打电话给... $('#error').show(); 让它可见! 编辑 :我明白你在做什么。 我建议您使用JSON作为您的数据类型。 请尝试以下方法 PHP
  • 自3.0版以来,BotDetect验证码支持中文字符http://captcha.biz/localizations/chinese-captcha.html 几天前他们也发布了PHP版本。 http://captcha.biz/php-captcha.html 它适用于禁用的JavaScript。 BotDetect captcha supports Chinese characters since the version 3.0 http://captcha.biz/localizations/chin ...
  • 我想在处理验证码时无法避免处理渲染图像? 这是一个简单的(可能不是最优雅的): session_start(); $strings = '123456789'; $i = 0; $characters = 6; $code = ''; while ($i < $characters) { $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1); $i++; } $_SESSION['captcha'] = $code; ...
  • 我不确定CAPTCHA会如何防范CSRF。 CSRF在用户使用安全站点进行身份验证时发生,然后访问恶意站点,然后通过利用该经过身份验证的连接的表单,映像和AJAX向安全站点提交请求。 CAPTCHA仅确保某人是人,通常用于确保评论者或新注册人是人,而不是机器人。 我永远不会依赖CAPTCHA来获得任何安全性,因为现在机器人在阅读其中一些时比人类更好。 I'm unsure how a CAPTCHA would provide any guard against CSRF. CSRF occurs when ...
  • 您还可以输出看起来像常规字符(但不是)的Unicode字符,并要求用户重复这些字符。 西里尔字母有许多看起来很熟悉的拉丁文字符,但编码方式不同: А vs A В vs B Е vs E Ѕ vs S І vs I К vs K М vs M Н vs H О vs O Р vs P Т vs T Х vs X 这很容易破解,但易于实现,易于用户解决。 (关于你的代码示例:跳过整个会话事件,只需从captcha.php文件中回显CAPTCHA。确 ...
  • 不要只依赖SESSION值,原因有两个: 您的会话可能会过期,因此在某些情况下无法使用 如果用户打开具有相同页面的另一个选项卡,您将有一个奇怪的行为 使用某种令牌: 输出表单时生成随机ID,将其放入数据库中,并附上预期的数字(以及当前日期/时间) 使用此ID生成图像 使用ID在表单中添加隐藏的输入 收到POST后,从数据库中获取预期值并进行比较 删除此标记和所有旧标记(例如WHERE token == %token AND datetime < DATE_SUB(NOW(), INTERVAL 1 HOUR ...
  • 您可以使用下面的代码使用PHP的gd库实现一个简单的验证码。 作为初学者,这里有一个快速测试的示例代码,它还包括字体大小:

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果