如何在postgres中添加枚举字段?

时间:2019-03-14 12:33:31

标签: spring postgresql

在我的模型中有一个枚举字段,试图向数据库中添加一个枚举字段,但是不起作用,您可以发表评论吗?

MyApp

我的邮编

public class MyApp extends Application {

private PrefManager prefManager;
@Override
public void onCreate() {
    super.onCreate();
    prefManager = new PrefManager(this);
    if (prefManager.isNotifOns()) {
        getClipTexts();
    }
}



//Listener to listen change in clipboard
private void getClipTexts() {
    final ClipboardManager clipboardManager = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {

            try {

                String textToPaste = clipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
                if (textToPaste.length() > 200) {

                    makeNotification(textToPaste);


                }
            } catch (Exception ignored) {

            }


        }
    });
}


//Creating notifications after copying the clipboard text
public void makeNotification(String input) {


    Intent notifIntent = new Intent(getApplicationContext(), PostActivity.class);
    notifIntent.putExtra("post", input);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationId = 1;
    String channelId = "channel-01";
    String channelName = "Copy to Post";
    int importance = 0;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        importance = NotificationManager.IMPORTANCE_DEFAULT;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Post copied content")
            .setContentText(input)
            .setAutoCancel(true);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    stackBuilder.addNextIntent(notifIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);

    notificationManager.notify(notificationId, mBuilder.build());


}
}

我想我在postgres中错误地声明了变量enum

2 个答案:

答案 0 :(得分:1)

使用枚举注释,Spring会为您处理。 但一般来说,Postgres中的Enums将是VARCHAR(X)(请参阅第二个链接)

public class ChatMessage {
    @Enumerated(EnumType.STRING)
    private MessageType messageType;
    private String content;
    private String sender;
}

http://tomee.apache.org/examples-trunk/jpa-enumerated/

https://vladmihalcea.com/the-best-way-to-map-an-enum-type-with-jpa-and-hibernate/

答案 1 :(得分:0)

如果我做对了,according to the documentation很好。

尽管在他们的示例中,他们为引用枚举类型的列选择了不同的名称。 尝试更改它,例如messages messageType

另一个示例:https://tapoueh.org/blog/2018/05/postgresql-data-types-enum/#postgresql-enum-data-type